From 076838de2697a4185125bfd601fbcd6e7286bfb6 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Tue, 2 Jun 2026 15:52:08 +0200 Subject: [PATCH] src: shorten namespace for private functions and macros From `_libssh2_*` to `ssh2_*`, `LIBSSH2_*` -> `SSH2_*`. To make the code easier to read, write, to fit more into the maximum line width without folding, and to avoid using reserved namespace (`_*`). Namespacing after this patch: - `libssh2_`, `LIBSSH2_` (rarely preceded with `_`): public API. - `ssh2_`, `SSH2_`: private API. - `ssh2__`: non-static crypto functions. where `` is: lgcr, mbed, ossl, wcng, os400qc3 - local macros and static functions try to be short, descriptive of their scope, without strict namespacing. They also avoid namespaces used above for clarity. Also: - reflow using clang-format (and manually where necessary.) - merge split error strings where possible (manually). - shorten `_libssh2_debug()` to `ssh2_deb()`. - shorten `_libssh2_error()` to `ssh2_err()`. - move `libssh2_*` internal symbols to the `ssh2_*` namespace. - shorten local macro/enum prefixes from `LIBSSH2_` to `SSH2_`. - mbedtls, wincng: move static functions to the local namespace, where missing. - drop stray no-op `ssh2_init_aes_ctr()` symbols (libgcrypt, mbedtls). - move crypto primitives to crypto-local namespace `ssh2_(lgcr|mbed|ossl|wcng)_`, where missing. - normalize crypo-local and crypto-static function namespace to: `mbed_*`, `ssh2_mbed_,*` `wcng_*`, `ssh2_wcng_*`. - `LIBSSH2_CIPHER_T()` -> `SSH2_CIPHER_T()` Follow-up to 689b20232070662daba003c7993585bd57fd92a7 #1923 - rename `ssh2_store_bignum2_bytes()` -> `ssh2_store_bignum_bytes()`. - drop redundant `void *` cast from `SSH2_UNCONST()` values. - os400qc3: drop unused static `ssh2_bn_from_bn()`. - os400qc3: move static `ssh2_bn_resize()` off the ssh2 namespace. - libssh2.rpgle.in: drop private macros `LIBSSH2_DH_GEX*`. - rename `ARRAY_SIZE()` to `SSH2_ARRAYSIZE()` for uniqueness. - agent: drop unused/commented `SSH_AGENTC_*` protocol 1 macros. - fix stray symbol references found in comments. - minor whitespace fixes along the way. Note: - `ssh2` as a namespace has the minor downside of potential confusion with SSH protocol 2 (vs 1). In practice I think this is fine. - `SSH2_` prefix has been used in two places before this patch: 4 `SSH2_AGENT*` macros in `agent.c` and 2 `SSH2_MSG_KEX_ECDH_*` macros in `libssh2_priv.h`. The latter is coming from https://datatracker.ietf.org/doc/html/rfc9941. If this ever creates confusion they can be moved under an `IETF_` namespace for example. - libssh2_userauth_publickey_sk: the public man page says the callbacks needs to be using `LIBSSH2_ALLOC()` for allocation and responsible for freeing. It's an internal macro and should not be mentioned or necessary in public docs. Ref: ed439a29bb0b4d1c3f681f87ccfcd3e5a66c3ba0 #698 Follow-up to 8c9b7771ee9ee6b55e0c340bd519fe74a4b5fd1a #1931 Follow-up to 689b20232070662daba003c7993585bd57fd92a7 #1923 Ref: #1949 Closes #1961 --- docs/HACKING-CRYPTO | 635 +++--- docs/libssh2_userauth_publickey_sk.md | 4 +- os400/ccsid.c | 6 +- os400/libssh2rpg/libssh2.rpgle.in | 9 - src/agent.c | 321 ++- src/bcrypt_pbkdf.c | 48 +- src/channel.c | 1486 +++++++------- src/channel.h | 56 +- src/cipher-chachapoly.c | 6 +- src/comp.c | 74 +- src/comp.h | 2 +- src/crypt.c | 66 +- src/crypto.h | 426 ++-- src/global.c | 6 +- src/hostkey.c | 391 ++-- src/keepalive.c | 8 +- src/kex.c | 2444 +++++++++++------------ src/knownhost.c | 263 ++- src/libgcrypt.c | 348 ++-- src/libgcrypt.h | 196 +- src/libssh2_priv.h | 415 ++-- src/mac.c | 97 +- src/mac.h | 5 +- src/mbedtls.c | 482 +++-- src/mbedtls.h | 373 ++-- src/misc.c | 212 +- src/misc.h | 114 +- src/openssl.c | 1507 +++++++------- src/openssl.h | 292 ++- src/os400qc3.c | 501 +++-- src/os400qc3.h | 231 +-- src/packet.c | 1087 +++++----- src/packet.h | 53 +- src/pem.c | 386 ++-- src/publickey.c | 637 +++--- src/scp.c | 485 +++-- src/session.c | 475 +++-- src/session.h | 8 +- src/sftp.c | 1899 +++++++++--------- src/sftp.h | 44 +- src/transport.c | 229 ++- src/transport.h | 14 +- src/userauth.c | 1317 ++++++------ src/userauth.h | 2 +- src/userauth_kbd_packet.c | 106 +- src/version.c | 2 +- src/wincng.c | 1540 +++++++------- src/wincng.h | 460 ++--- tests/openssh_fixture.c | 4 +- tests/test_auth_keyboard_info_request.c | 4 +- tests/test_hostkey.c | 22 +- tests/test_simple.c | 10 +- 52 files changed, 9588 insertions(+), 10220 deletions(-) diff --git a/docs/HACKING-CRYPTO b/docs/HACKING-CRYPTO index 75d0b4e9..23bc75ee 100644 --- a/docs/HACKING-CRYPTO +++ b/docs/HACKING-CRYPTO @@ -42,10 +42,10 @@ elif test "$found_crypto" = "newname"; then 1) Crypto library initialization/termination. -void libssh2_crypto_init(void); +void ssh2_crypto_init(void); Initializes the crypto library. May be an empty macro if not needed. -void libssh2_crypto_exit(void); +void ssh2_crypto_exit(void); Terminates the crypto library use. May be an empty macro if not needed. 1.1) Crypto runtime detection @@ -55,27 +55,25 @@ libssh2_crypto_engine() must return it when it is built in. 2) HMAC -libssh2_hmac_ctx +ssh2_hmac_ctx Type of an HMAC computation context. Generally a struct. Used for all hash algorithms. -int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx); +int ssh2_hmac_ctx_init(ssh2_hmac_ctx *ctx); Initializes the HMAC computation context ctx. Called before setting-up the hash algorithm. Must return 1 for success and 0 for failure. -int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, - const void *data, int datalen); +int ssh2_hmac_update(ssh2_hmac_ctx *ctx, const void *data, int datalen); Continue computation of an HMAC on datalen bytes at data using context ctx. Must return 1 for success and 0 for failure. -int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, - void output[]); +int ssh2_hmac_final(ssh2_hmac_ctx *ctx, void output[]); Get the computed HMAC from context ctx into the output buffer. The minimum data buffer size depends on the HMAC hash algorithm. Must return 1 for success and 0 for failure. -void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx); +void ssh2_hmac_cleanup(ssh2_hmac_ctx *ctx); Releases the HMAC computation context at ctx. 3) Hash algorithms. @@ -86,23 +84,23 @@ Must always be implemented. SHA_DIGEST_LENGTH #define to 20, the SHA-1 digest length. -libssh2_sha1_ctx +ssh2_sha1_ctx Type of an SHA-1 computation context. Generally a struct. -int libssh2_sha1_init(libssh2_sha1_ctx *x); +int ssh2_sha1_init(ssh2_sha1_ctx *x); Initializes the SHA-1 computation context at x. Returns 1 for success and 0 for failure -int libssh2_sha1_update(libssh2_sha1_ctx ctx, - const unsigned char *data, - size_t len); +int ssh2_sha1_update(ssh2_sha1_ctx ctx, + const unsigned char *data, + size_t len); Continue computation of SHA-1 on len bytes at data using context ctx. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_sha1_final(libssh2_sha1_ctx ctx, - unsigned char output[SHA_DIGEST_LEN]); +int ssh2_sha1_final(ssh2_sha1_ctx ctx, + unsigned char output[SHA_DIGEST_LEN]); Get the computed SHA-1 signature from context ctx and store it into the output buffer. Release the context. @@ -110,11 +108,11 @@ Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, - const void *key, - int keylen); +int ssh2_hmac_sha1_init(ssh2_hmac_ctx *ctx, + const void *key, + int keylen); Setup the HMAC computation context ctx for an HMAC-SHA-1 computation using the -keylen-byte key. Is invoked right after libssh2_hmac_ctx_init(). +keylen-byte key. Is invoked right after ssh2_hmac_ctx_init(). Returns 1 for success and 0 for failure. 3.2) SHA-256 @@ -123,32 +121,32 @@ Must always be implemented. SHA256_DIGEST_LENGTH #define to 32, the SHA-256 digest length. -libssh2_sha256_ctx +ssh2_sha256_ctx Type of an SHA-256 computation context. Generally a struct. -int libssh2_sha256_init(libssh2_sha256_ctx *x); +int ssh2_sha256_init(ssh2_sha256_ctx *x); Initializes the SHA-256 computation context at x. Returns 1 for success and 0 for failure -int libssh2_sha256_update(libssh2_sha256_ctx ctx, - const unsigned char *data, - size_t len); +int ssh2_sha256_update(ssh2_sha256_ctx ctx, + const unsigned char *data, + size_t len); Continue computation of SHA-256 on len bytes at data using context ctx. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_sha256_final(libssh2_sha256_ctx ctx, - unsigned char output[SHA256_DIGEST_LENGTH]); +int ssh2_sha256_final(ssh2_sha256_ctx ctx, + unsigned char output[SHA256_DIGEST_LENGTH]); Gets the computed SHA-256 signature from context ctx into the output buffer. Release the context. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_sha256(const unsigned char *message, - size_t len, - unsigned char output[SHA256_DIGEST_LENGTH]); +int ssh2_sha256(const unsigned char *message, + size_t len, + unsigned char output[SHA256_DIGEST_LENGTH]); Computes the SHA-256 signature over the given message of length len and store the result into the output buffer. Return 1 if error, else 0. @@ -158,11 +156,11 @@ LIBSSH2_HMAC_SHA256 #define as 1 if the crypto library supports HMAC-SHA-256, else 0. If defined as 0, the rest of this section can be omitted. -int libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, - const void *key, - int keylen); +int ssh2_hmac_sha256_init(ssh2_hmac_ctx *ctx, + const void *key, + int keylen); Setup the HMAC computation context ctx for an HMAC-256 computation using the -keylen-byte key. Is invoked right after libssh2_hmac_ctx_init(). +keylen-byte key. Is invoked right after ssh2_hmac_ctx_init(). Returns 1 for success and 0 for failure. 3.3) SHA-384 @@ -171,32 +169,32 @@ Mandatory if ECDSA is implemented. Can be omitted otherwise. SHA384_DIGEST_LENGTH #define to 48, the SHA-384 digest length. -libssh2_sha384_ctx +ssh2_sha384_ctx Type of an SHA-384 computation context. Generally a struct. -int libssh2_sha384_init(libssh2_sha384_ctx *x); +int ssh2_sha384_init(ssh2_sha384_ctx *x); Initializes the SHA-384 computation context at x. Returns 1 for success and 0 for failure -int libssh2_sha384_update(libssh2_sha384_ctx ctx, - const unsigned char *data, - size_t len); +int ssh2_sha384_update(ssh2_sha384_ctx ctx, + const unsigned char *data, + size_t len); Continue computation of SHA-384 on len bytes at data using context ctx. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_sha384_final(libssh2_sha384_ctx ctx, - unsigned char output[SHA384_DIGEST_LENGTH]); +int ssh2_sha384_final(ssh2_sha384_ctx ctx, + unsigned char output[SHA384_DIGEST_LENGTH]); Gets the computed SHA-384 signature from context ctx into the output buffer. Release the context. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_sha384(const unsigned char *message, - size_t len, - unsigned char output[SHA384_DIGEST_LENGTH]); +int ssh2_sha384(const unsigned char *message, + size_t len, + unsigned char output[SHA384_DIGEST_LENGTH]); Computes the SHA-384 signature over the given message of length len and store the result into the output buffer. Return 1 if error, else 0. @@ -207,32 +205,32 @@ Must always be implemented. SHA512_DIGEST_LENGTH #define to 64, the SHA-512 digest length. -libssh2_sha512_ctx +ssh2_sha512_ctx Type of an SHA-512 computation context. Generally a struct. -int libssh2_sha512_init(libssh2_sha512_ctx *x); +int ssh2_sha512_init(ssh2_sha512_ctx *x); Initializes the SHA-512 computation context at x. Returns 1 for success and 0 for failure -int libssh2_sha512_update(libssh2_sha512_ctx ctx, - const unsigned char *data, - size_t len); +int ssh2_sha512_update(ssh2_sha512_ctx ctx, + const unsigned char *data, + size_t len); Continue computation of SHA-512 on len bytes at data using context ctx. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_sha512_final(libssh2_sha512_ctx ctx, - unsigned char output[SHA512_DIGEST_LENGTH]); +int ssh2_sha512_final(ssh2_sha512_ctx ctx, + unsigned char output[SHA512_DIGEST_LENGTH]); Gets the computed SHA-512 signature from context ctx into the output buffer. Release the context. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_sha512(const unsigned char *message, - size_t len, - unsigned char output[SHA512_DIGEST_LENGTH]); +int ssh2_sha512(const unsigned char *message, + size_t len, + unsigned char output[SHA512_DIGEST_LENGTH]); Computes the SHA-512 signature over the given message of length len and store the result into the output buffer. Return 1 if error, else 0. @@ -242,11 +240,11 @@ LIBSSH2_HMAC_SHA512 #define as 1 if the crypto library supports HMAC-SHA-512, else 0. If defined as 0, the rest of this section can be omitted. -int libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, - const void *key, - int keylen); +int ssh2_hmac_sha512_init(ssh2_hmac_ctx *ctx, + const void *key, + int keylen); Setup the HMAC computation context ctx for an HMAC-512 computation using the -keylen-byte key. Is invoked right after libssh2_hmac_ctx_init(). +keylen-byte key. Is invoked right after ssh2_hmac_ctx_init(). Returns 1 for success and 0 for failure. 3.5) MD5 @@ -257,35 +255,35 @@ If defined as 0, the rest of this section can be omitted. MD5_DIGEST_LENGTH #define to 16, the MD5 digest length. -libssh2_md5_ctx +ssh2_md5_ctx Type of an MD5 computation context. Generally a struct. -int libssh2_md5_init(libssh2_md5_ctx *x); +int ssh2_md5_init(ssh2_md5_ctx *x); Initializes the MD5 computation context at x. Returns 1 for success and 0 for failure -int libssh2_md5_update(libssh2_md5_ctx ctx, - const unsigned char *data, - size_t len); +int ssh2_md5_update(ssh2_md5_ctx ctx, + const unsigned char *data, + size_t len); Continues computation of MD5 on len bytes at data using context ctx. Returns 1 for success and 0 for failure. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_md5_final(libssh2_md5_ctx ctx, - unsigned char output[MD5_DIGEST_LENGTH]); +int ssh2_md5_final(ssh2_md5_ctx ctx, + unsigned char output[MD5_DIGEST_LENGTH]); Gets the computed MD5 signature from context ctx into the output buffer. Release the context. Note: if the ctx parameter is modified by the underlying code, this procedure must be implemented as a macro to map ctx --> &ctx. Must return 1 for success and 0 for failure. -int libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, - const void *key, - int keylen); +int ssh2_hmac_md5_init(ssh2_hmac_ctx *ctx, + const void *key, + int keylen); Setup the HMAC computation context ctx for an HMAC-MD5 computation using the -keylen-byte key. Is invoked right after libssh2_hmac_ctx_init(). +keylen-byte key. Is invoked right after ssh2_hmac_ctx_init(). Returns 1 for success and 0 for failure. 3.6) RIPEMD-160 @@ -293,45 +291,45 @@ LIBSSH2_HMAC_RIPEMD #define as 1 if the crypto library supports HMAC-RIPEMD-160, else 0. If defined as 0, the rest of this section can be omitted. -int libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx, - const void *key, - int keylen); +int ssh2_hmac_ripemd160_init(ssh2_hmac_ctx *ctx, + const void *key, + int keylen); Setup the HMAC computation context ctx for an HMAC-RIPEMD-160 computation using -the keylen-byte key. Is invoked right after libssh2_hmac_ctx_init(). +the keylen-byte key. Is invoked right after ssh2_hmac_ctx_init(). Returns 1 for success and 0 for failure. 4) Bidirectional key ciphers. -libssh2_cipher_ctx +ssh2_cipher_ctx Type of a cipher computation context. -LIBSSH2_CIPHER_T(name); +SSH2_CIPHER_T(name); Macro defining name as storage identifying a cipher algorithm for the crypto library interface. No trailing semicolon. -int _libssh2_cipher_init(libssh2_cipher_ctx *h, - LIBSSH2_CIPHER_T(algo), - unsigned char *iv, - unsigned char *secret, - int encrypt); +int ssh2_cipher_init(ssh2_cipher_ctx *h, + SSH2_CIPHER_T(algo), + unsigned char *iv, + unsigned char *secret, + int encrypt); Creates a cipher context for the given algorithm with the initialization vector iv and the secret key secret. Prepare for encryption or decryption depending on encrypt. Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, - LIBSSH2_CIPHER_T(algo), - int encrypt, - unsigned char *block, - size_t blocksize, - int firstlast); +int ssh2_cipher_crypt(ssh2_cipher_ctx *ctx, + SSH2_CIPHER_T(algo), + int encrypt, + unsigned char *block, + size_t blocksize, + int firstlast); Encrypt or decrypt in-place data at (block, blocksize) using the given context and/or algorithm. Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -void _libssh2_cipher_dtor(libssh2_cipher_ctx *ctx); +void ssh2_cipher_dtor(ssh2_cipher_ctx *ctx); Release cipher context at ctx. 4.1) AES @@ -340,115 +338,115 @@ LIBSSH2_AES #define as 1 if the crypto library supports AES in CBC mode, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_cipher_aes128 +ssh2_cipher_aes128 AES-128-CBC algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). -libssh2_cipher_aes192 +ssh2_cipher_aes192 AES-192-CBC algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). -libssh2_cipher_aes256 +ssh2_cipher_aes256 AES-256-CBC algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). 4.1.2) AES in CTR block mode. LIBSSH2_AES_CTR #define as 1 if the crypto library supports AES in CTR mode, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_cipher_aes128ctr +ssh2_cipher_aes128ctr AES-128-CTR algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). -libssh2_cipher_aes192ctr +ssh2_cipher_aes192ctr AES-192-CTR algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). -libssh2_cipher_aes256ctr +ssh2_cipher_aes256ctr AES-256-CTR algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). 4.2) Blowfish in CBC block mode. LIBSSH2_BLOWFISH #define as 1 if the crypto library supports blowfish in CBC mode, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_cipher_blowfish +ssh2_cipher_blowfish Blowfish-CBC algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). 4.3) RC4. LIBSSH2_RC4 #define as 1 if the crypto library supports RC4 (arcfour), else 0. If defined as 0, the rest of this section can be omitted. -libssh2_cipher_arcfour +ssh2_cipher_arcfour RC4 algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). 4.4) CAST5 in CBC block mode. LIBSSH2_CAST #define 1 if the crypto library supports cast, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_cipher_cast5 +ssh2_cipher_cast5 CAST5-CBC algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). 4.5) Triple DES in CBC block mode. LIBSSH2_3DES #define as 1 if the crypto library supports TripleDES in CBC mode, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_cipher_3des +ssh2_cipher_3des TripleDES-CBC algorithm identifier initializer. -#define with constant value of type LIBSSH2_CIPHER_T(). +#define with constant value of type SSH2_CIPHER_T(). 5) Diffie-Hellman support. -LIBSSH2_DH_GEX_MINGROUP +SSH2_DH_GEX_MINGROUP The minimum Diffie-Hellman group length in bits supported by the backend. Usually defined as 2048. -LIBSSH2_DH_GEX_OPTGROUP +SSH2_DH_GEX_OPTGROUP The preferred Diffie-Hellman group length in bits. Usually defined as 4096. -LIBSSH2_DH_GEX_MAXGROUP +SSH2_DH_GEX_MAXGROUP The maximum Diffie-Hellman group length in bits supported by the backend. Usually defined as 8192. -LIBSSH2_DH_MAX_MODULUS_BITS +SSH2_DH_MAX_MODULUS_BITS The maximum Diffie-Hellman modulus bit count accepted from the server. This value must be supported by the backend. Usually 16384. 5.1) Diffie-Hellman context. -libssh2_dh_ctx +ssh2_dh_ctx Type of a Diffie-Hellman computation context. Must always be defined. 5.2) Diffie-Hellman computation procedures. -void libssh2_dh_init(libssh2_dh_ctx *dhctx); +void ssh2_dh_init(ssh2_dh_ctx *dhctx); Initializes the Diffie-Hellman context at `dhctx'. No effective context creation needed here. -int libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, int group_order, - libssh2_bn_ctx *bnctx); +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); 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. The private key is stored as opaque in the Diffie-Hellman context `*dhctx' and the public key is returned in `public'. 0 is returned upon success, else -1. -int libssh2_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p, libssh2_bn_ctx *bnctx) +int ssh2_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, 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 at context creation. The result is stored in `secret'. 0 is returned upon success, else -1. -void libssh2_dh_dtor(libssh2_dh_ctx *dhctx) +void ssh2_dh_dtor(ssh2_dh_ctx *dhctx) Destroys Diffie-Hellman context at `dhctx' and resets its storage. 6) Big numbers. @@ -458,54 +456,54 @@ Positive multi-byte integers support is sufficient. This has a real meaning if the big numbers computations need some context storage. If not, use a dummy type and functions (macros). -libssh2_bn_ctx +ssh2_bn_ctx Type of multiple precision computation context. May not be empty. if not used, #define as char, for example. -libssh2_bn_ctx _libssh2_bn_ctx_new(void); +ssh2_bn_ctx ssh2_bn_ctx_new(void); Returns a new multiple precision computation context. -void _libssh2_bn_ctx_free(libssh2_bn_ctx ctx); +void ssh2_bn_ctx_free(ssh2_bn_ctx ctx); Releases a multiple precision computation context. 6.2) Computation support. -libssh2_bn +ssh2_bn Type of multiple precision numbers (aka bignumbers or huge integers) for the crypto library. -libssh2_bn *_libssh2_bn_init(void); +ssh2_bn *ssh2_bn_init(void); Creates a multiple precision number (preset to zero). -libssh2_bn *_libssh2_bn_init_from_bin(void); +ssh2_bn *ssh2_bn_init_from_bin(void); Create a multiple precision number intended to be set by the -_libssh2_bn_from_bin() function (see below). Unlike _libssh2_bn_init(), this -code may be a dummy initializer if the _libssh2_bn_from_bin() actually -allocates the number. Returns a value of type libssh2_bn *. +ssh2_bn_from_bin() function (see below). Unlike ssh2_bn_init(), this +code may be a dummy initializer if the ssh2_bn_from_bin() actually +allocates the number. Returns a value of type ssh2_bn *. -void _libssh2_bn_free(libssh2_bn *bn); +void ssh2_bn_free(ssh2_bn *bn); Destroys the multiple precision number at bn. -unsigned long _libssh2_bn_bytes(libssh2_bn *bn); +unsigned long ssh2_bn_bytes(ssh2_bn *bn); Get the number of bytes needed to store the bits of the multiple precision number at bn. -unsigned long _libssh2_bn_bits(libssh2_bn *bn); +unsigned long ssh2_bn_bits(ssh2_bn *bn); Returns the number of bits of multiple precision number at bn. -int _libssh2_bn_set_word(libssh2_bn *bn, unsigned long val); +int ssh2_bn_set_word(ssh2_bn *bn, unsigned long val); Sets the value of bn to val. Returns 1 on success, 0 otherwise. -libssh2_bn *_libssh2_bn_from_bin(libssh2_bn *bn, int len, - const unsigned char *val); +ssh2_bn *ssh2_bn_from_bin(ssh2_bn *bn, int len, + const unsigned char *val); Converts the positive integer in big-endian form of length len at val -into a libssh2_bn and place it in bn. If bn is NULL, a new libssh2_bn is +into a ssh2_bn and place it in bn. If bn is NULL, a new ssh2_bn is created. -Returns a pointer to target libssh2_bn or NULL if error. +Returns a pointer to target ssh2_bn or NULL if error. -int _libssh2_bn_to_bin(libssh2_bn *bn, unsigned char *val); +int ssh2_bn_to_bin(ssh2_bn *bn, unsigned char *val); Converts the absolute value of bn into big-endian form and store it at -val. val must point to _libssh2_bn_bytes(bn) bytes of memory. +val. val must point to ssh2_bn_bytes(bn) bytes of memory. Returns the length of the big-endian number. 7) Private key algorithms. @@ -534,32 +532,32 @@ a) "ssh-ed25519". b) raw key (32 bytes). Each item is preceded by its 32-bit byte length, MSB first. -int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekey, - const char *passphrase); +int ssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekey, + const char *passphrase); Reads a private key from file privatekey and extract the public key --> (pubkeydata, pubkeydata_len). Store the associated method (ssh-rsa or ssh-dss) into (method, method_len). -Both buffers have to be allocated using LIBSSH2_ALLOC(). +Both buffers have to be allocated using SSH2_ALLOC(). Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase); +int ssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase); Gets a private key from bytes at (privatekeydata, privatekeydata_len) and extract the public key --> (pubkeydata, pubkeydata_len). Store the associated method (ssh-rsa or ssh-dss) into (method, method_len). -Both buffers have to be allocated using LIBSSH2_ALLOC(). +Both buffers have to be allocated using SSH2_ALLOC(). Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. @@ -568,25 +566,18 @@ LIBSSH2_RSA #define as 1 if the crypto library supports RSA, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_rsa_ctx +ssh2_rsa_ctx Type of an RSA computation context. Generally a struct. -int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, - const unsigned char *edata, - unsigned long elen, - const unsigned char *ndata, - unsigned long nlen, - const unsigned char *ddata, - unsigned long dlen, - const unsigned char *pdata, - unsigned long plen, - const unsigned char *qdata, - unsigned long qlen, - const unsigned char *e1data, - unsigned long e1len, - const unsigned char *e2data, - unsigned long e2len, - const unsigned char *coeffdata, unsigned long coefflen); +int ssh2_rsa_new(ssh2_rsa_ctx **rsa, + const unsigned char *edata, unsigned long elen, + const unsigned char *ndata, unsigned long nlen, + const unsigned char *ddata, unsigned long dlen, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *e1data, unsigned long e1len, + const unsigned char *e2data, unsigned long e2len, + const unsigned char *coeffdata, unsigned long coefflen); Creates a new context for RSA computations from key source values: pdata, plen Prime number p. Only used if private key known (ddata). qdata, qlen Prime number q. Only used if private key known (ddata). @@ -602,103 +593,103 @@ Note: the current generic code only calls this function with e and n (public key parameters): unless used internally by the backend, it is not needed to support the private key and the other parameters here. -int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); +int ssh2_rsa_new_private(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); Reads an RSA private key from file filename into a new RSA context. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *data, - size_t data_len, - const unsigned char *passphrase); +int ssh2_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *data, + size_t data_len, + const unsigned char *passphrase); Gets an RSA private key from data into a new RSA context. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx *rsa, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len); +int ssh2_rsa_sha1_verify(ssh2_rsa_ctx *rsa, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len); Verify (sig, sig_len) signature of (m, m_len) using an SHA-1 hash and the RSA context. Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_rsa_sha1_signv(LIBSSH2_SESSION *session, - unsigned char **sig, size_t *siglen, - int count, const struct iovec vector[], - libssh2_rsa_ctx *ctx); +int ssh2_rsa_sha1_signv(LIBSSH2_SESSION *session, + unsigned char **sig, size_t *siglen, + int count, const struct iovec vector[], + ssh2_rsa_ctx *ctx); RSA signs the SHA-1 hash computed over the count data chunks in vector. Signature is stored at (sig, siglen). Signature buffer must be allocated from the given session. Returns 0 if OK, else -1. Note: this procedure is optional: if provided, it MUST be defined as a macro. -int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len); +int ssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len); RSA signs the (hash, hashlen) SHA-1 hash bytes and stores the allocated signature at (signature, signature_len). Signature buffer must be allocated from the given session. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -Note: this procedure is not used if macro _libssh2_rsa_sha1_signv() is defined. +Note: this procedure is not used if macro ssh2_rsa_sha1_signv() is defined. -void _libssh2_rsa_free(libssh2_rsa_ctx *rsactx); +void ssh2_rsa_free(ssh2_rsa_ctx *rsactx); Releases the RSA computation context at rsactx. LIBSSH2_RSA_SHA2 #define as 1 if the crypto library supports RSA SHA2 256/512, else 0. If defined as 0, the rest of this section can be omitted. -int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len); +int ssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len); RSA signs the (hash, hashlen) SHA-2 hash bytes based on hash length and stores the allocated signature at (signature, signature_len). Signature buffer must be allocated from the given session. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -Note: this procedure is not used if both macros _libssh2_rsa_sha2_256_signv() -and _libssh2_rsa_sha2_512_signv are defined. +Note: this procedure is not used if both macros ssh2_rsa_sha2_256_signv() +and ssh2_rsa_sha2_512_signv are defined. -int _libssh2_rsa_sha2_256_signv(LIBSSH2_SESSION *session, - unsigned char **sig, size_t *siglen, - int count, const struct iovec vector[], - libssh2_rsa_ctx *ctx); +int ssh2_rsa_sha2_256_signv(LIBSSH2_SESSION *session, + unsigned char **sig, size_t *siglen, + int count, const struct iovec vector[], + ssh2_rsa_ctx *ctx); RSA signs the SHA-256 hash computed over the count data chunks in vector. Signature is stored at (sig, siglen). Signature buffer must be allocated from the given session. Returns 0 if OK, else -1. Note: this procedure is optional: if provided, it MUST be defined as a macro. -int _libssh2_rsa_sha2_512_signv(LIBSSH2_SESSION *session, - unsigned char **sig, size_t *siglen, - int count, const struct iovec vector[], - libssh2_rsa_ctx *ctx); +int ssh2_rsa_sha2_512_signv(LIBSSH2_SESSION *session, + unsigned char **sig, size_t *siglen, + int count, const struct iovec vector[], + ssh2_rsa_ctx *ctx); RSA signs the SHA-512 hash computed over the count data chunks in vector. Signature is stored at (sig, siglen). Signature buffer must be allocated from the given session. Returns 0 if OK, else -1. Note: this procedure is optional: if provided, it MUST be defined as a macro. -int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsa, - size_t hash_len, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len); +int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsa, + size_t hash_len, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len); Verify (sig, sig_len) signature of (m, m_len) using an SHA-2 hash based on hash length and the RSA context. Return 0 if OK, else -1. @@ -709,19 +700,15 @@ LIBSSH2_DSA #define as 1 if the crypto library supports DSA, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_dsa_ctx +ssh2_dsa_ctx Type of a DSA computation context. Generally a struct. -int _libssh2_dsa_new(libssh2_dsa_ctx **dsa, - const unsigned char *pdata, - unsigned long plen, - const unsigned char *qdata, - unsigned long qlen, - const unsigned char *gdata, - unsigned long glen, - const unsigned char *ydata, - unsigned long ylen, - const unsigned char *x, unsigned long x_len); +int ssh2_dsa_new(ssh2_dsa_ctx **dsa, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *gdata, unsigned long glen, + const unsigned char *ydata, unsigned long ylen, + const unsigned char *x, unsigned long x_len); Creates a new context for DSA computations from source key values: pdata, plen Prime number p. Only used if private key known (ddata). qdata, qlen Prime number q. Only used if private key known (ddata). @@ -731,68 +718,68 @@ Creates a new context for DSA computations from source key values: Returns 0 if OK. This procedure is already prototyped in crypto.h. -int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); +int ssh2_dsa_new_private(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); Gets a DSA private key from file filename into a new DSA context. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *data, - size_t data_len, - const unsigned char *passphrase); +int ssh2_dsa_new_private_frommemory(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *data, + size_t data_len, + const unsigned char *passphrase); Gets a DSA private key from the data_len-bytes data into a new DSA context. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, - const unsigned char *sig, - const unsigned char *m, size_t m_len); +int ssh2_dsa_sha1_verify(ssh2_dsa_ctx *dsactx, + const unsigned char *sig, + const unsigned char *m, size_t m_len); Verify (sig, siglen) signature of (m, m_len) using an SHA-1 hash and the DSA context. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx *dsactx, - const unsigned char *hash, - size_t hash_len, unsigned char *sig); +int ssh2_dsa_sha1_sign(ssh2_dsa_ctx *dsactx, + const unsigned char *hash, + size_t hash_len, unsigned char *sig); DSA signs the (hash, hash_len) data using SHA-1 and store the signature at sig. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -void _libssh2_dsa_free(libssh2_dsa_ctx *dsactx); +void ssh2_dsa_free(ssh2_dsa_ctx *dsactx); Releases the DSA computation context at dsactx. 7.3) ECDSA LIBSSH2_ECDSA #define as 1 if the crypto library supports ECDSA, else 0. -If defined as 0, libssh2_ec_key should be defined as void and the rest of +If defined as 0, ssh2_ec_key should be defined as void and the rest of this section can be omitted. EC_MAX_POINT_LEN Maximum point length. Usually defined as ((528 * 2 / 8) + 1) (= 133). -libssh2_ecdsa_ctx +ssh2_ecdsa_ctx Type of an ECDSA computation context. Generally a struct. -libssh2_ec_key +ssh2_ec_key Type of an elliptic curve key. -libssh2_curve_type +ssh2_curve_type An enum type defining curve types. Current supported identifiers are: - LIBSSH2_EC_CURVE_NISTP256 - LIBSSH2_EC_CURVE_NISTP384 - LIBSSH2_EC_CURVE_NISTP521 + SSH2_EC_CURVE_NISTP256 + SSH2_EC_CURVE_NISTP384 + SSH2_EC_CURVE_NISTP521 -int _libssh2_ecdsa_create_key(libssh2_ec_key **out_private_key, - unsigned char **out_public_key_octal, - size_t *out_public_key_octal_len, - libssh2_curve_type curve_type); +int ssh2_ecdsa_create_key(ssh2_ec_key **out_private_key, + unsigned char **out_public_key_octal, + size_t *out_public_key_octal_len, + ssh2_curve_type curve_type); Create a new ECDSA private key of type curve_type and return it at out_private_key. If out_public_key_octal is not NULL, store an allocated pointer to the associated public key in "octal" form in it and its length @@ -800,48 +787,48 @@ at out_public_key_octal_len. Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); +int ssh2_ecdsa_new_private(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); Reads an ECDSA private key from PEM file filename into a new ECDSA context. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase); +int ssh2_ecdsa_new_private_frommemory(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase); Builds an ECDSA private key from PEM data at filedata of length filedata_len into a new ECDSA context stored at ec_ctx. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx **ecdsactx, - const unsigned char *k, - size_t k_len, - libssh2_curve_type type); +int ssh2_ecdsa_curve_name_with_octal_new(ssh2_ecdsa_ctx **ecdsactx, + const unsigned char *k, + size_t k_len, + ssh2_curve_type type); Stores at ecdsactx a new ECDSA context associated with the given curve type and with "octal" form public key (k, k_len). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ecdsa_new_openssh_private(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); +int ssh2_ecdsa_new_openssh_private(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); Reads a PEM-encoded ECDSA private key from file filename encrypted with passphrase and stores at ec_ctx a new ECDSA context for it. Return 0 if OK, else -1. Currently used only from openssl backend (ought to be private). This procedure is already prototyped in crypto.h. -int _libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx, - const unsigned char *hash, unsigned long hash_len, - unsigned char **signature, size_t *signature_len); +int ssh2_ecdsa_sign(LIBSSH2_SESSION *session, ssh2_ecdsa_ctx *ec_ctx, + const unsigned char *hash, unsigned long hash_len, + unsigned char **signature, size_t *signature_len); ECDSA signs the (hash, hashlen) hash bytes and stores the allocated signature at (signature, signature_len). Hash algorithm used should be SHA-256, SHA-384 or SHA-512 depending on type stored in ECDSA context at ec_ctx. @@ -849,28 +836,28 @@ Signature buffer must be allocated from the given session. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ecdsa_verify(libssh2_ecdsa_ctx *ctx, - const unsigned char *r, size_t r_len, - const unsigned char *s, size_t s_len, - const unsigned char *m, size_t m_len); +int ssh2_ecdsa_verify(ssh2_ecdsa_ctx *ctx, + const unsigned char *r, size_t r_len, + const unsigned char *s, size_t s_len, + const unsigned char *m, size_t m_len); Verify the ECDSA signature made of (r, r_len) and (s, s_len) of (m, m_len) using the hash algorithm configured in the ECDSA context ctx. Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -libssh2_curve_type _libssh2_ecdsa_get_curve_type(libssh2_ecdsa_ctx *ecdsactx); +ssh2_curve_type ssh2_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ecdsactx); Returns the curve type associated with given context. This procedure is already prototyped in crypto.h. -int _libssh2_ecdsa_curve_type_from_name(const char *name, - libssh2_curve_type *out_type); +int ssh2_ecdsa_curve_type_from_name(const char *name, + ssh2_curve_type *out_type); Stores in out_type the curve type matching string name of the form "ecdsa-sha2-nistpxxx". Return 0 if OK, else -1. Currently used only from openssl backend (ought to be private). This procedure is already prototyped in crypto.h. -void _libssh2_ecdsa_free(libssh2_ecdsa_ctx *ecdsactx); +void ssh2_ecdsa_free(ssh2_ecdsa_ctx *ecdsactx); Releases the ECDSA computation context at ecdsactx. 7.4) ED25519 @@ -878,93 +865,93 @@ LIBSSH2_ED25519 #define as 1 if the crypto library supports ED25519, else 0. If defined as 0, the rest of this section can be omitted. -libssh2_ed25519_ctx +ssh2_ed25519_ctx Type of an ED25519 computation context. Generally a struct. -int _libssh2_curve25519_new(LIBSSH2_SESSION *session, libssh2_ed25519_ctx **ctx, - uint8_t **out_public_key, - uint8_t **out_private_key); +int ssh2_curve25519_new(LIBSSH2_SESSION *session, ssh2_ed25519_ctx **ctx, + uint8_t **out_public_key, + uint8_t **out_private_key); Generates an ED25519 key pair, stores a pointer to them at out_private_key and out_public_key respectively and stores at ctx a new ED25519 context for this key. Argument ctx, out_private_key and out_public key may be NULL to disable storing the corresponding value. -Length of each key is LIBSSH2_ED25519_KEY_LEN (32 bytes). +Length of each key is SSH2_ED25519_KEY_LEN (32 bytes). Key buffers are allocated and should be released by caller after use. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ed25519_new_private(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const uint8_t *passphrase); +int ssh2_ed25519_new_private(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const uint8_t *passphrase); Reads an ED25519 private key from PEM file filename into a new ED25519 context. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ed25519_new_public(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const unsigned char *raw_pub_key, - const size_t key_len); +int ssh2_ed25519_new_public(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const unsigned char *raw_pub_key, + const size_t key_len); Stores at ed_ctx a new ED25519 key context for raw public key (raw_pub_key, key_len). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase); +int ssh2_ed25519_new_private_frommemory(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase); Builds an ED25519 private key from PEM data at filedata of length filedata_len into a new ED25519 context stored at ed_ctx. -Must call _libssh2_init_if_needed(). +Must call ssh2_init_if_needed(). Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, - uint8_t **out_sig, size_t *out_sig_len, - const uint8_t *message, size_t message_len); +int ssh2_ed25519_sign(ssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, + uint8_t **out_sig, size_t *out_sig_len, + const uint8_t *message, size_t message_len); ED25519 signs the (message, message_len) bytes and stores the allocated signature at (sig, sig_len). Signature buffer is allocated from the given session. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_ed25519_verify(libssh2_ed25519_ctx *ctx, const uint8_t *s, - size_t s_len, const uint8_t *m, size_t m_len); +int ssh2_ed25519_verify(ssh2_ed25519_ctx *ctx, const uint8_t *s, + size_t s_len, const uint8_t *m, size_t m_len); Verify (s, s_len) signature of (m, m_len) using the given ED25519 context. Return 0 if OK, else -1. This procedure is already prototyped in crypto.h. -int _libssh2_curve25519_gen_k(libssh2_bn **k, - uint8_t private_key[LIBSSH2_ED25519_KEY_LEN], - uint8_t srvr_public_key[LIBSSH2_ED25519_KEY_LEN]); +int ssh2_curve25519_gen_k(ssh2_bn **k, + uint8_t private_key[SSH2_ED25519_KEY_LEN], + uint8_t srvr_public_key[SSH2_ED25519_KEY_LEN]); Computes a shared ED25519 secret key from the given raw server public key and raw client public key and stores it as a big number in *k. Big number should have been initialized before calling this function. Returns 0 if OK, else -1. This procedure is already prototyped in crypto.h. -void _libssh2_ed25519_free(libssh2_ed25519_ctx *ed25519ctx); +void ssh2_ed25519_free(ssh2_ed25519_ctx *ed25519ctx); Releases the ED25519 computation context at ed25519ctx. 8) Miscellaneous -void libssh2_prepare_iovec(struct iovec *vector, unsigned int len); +void ssh2_prepare_iovec(struct iovec *vector, unsigned int len); Prepare len consecutive iovec slots before using them. In example, this is needed to preset unused structure slacks on platforms requiring it. If this is not needed, it should be defined as an empty macro. -int _libssh2_random(unsigned char *buf, size_t len); +int ssh2_random(unsigned char *buf, size_t len); Store len random bytes at buf. Returns 0 if OK, else -1. -const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session, - unsigned char *key_method, - size_t key_method_len); +const char *ssh2_supported_key_sign_algs(LIBSSH2_SESSION *session, + unsigned char *key_method, + size_t key_method_len); This function is for implementing key hash upgrading as defined in RFC 8332. diff --git a/docs/libssh2_userauth_publickey_sk.md b/docs/libssh2_userauth_publickey_sk.md index 663c0f5c..b89291bf 100644 --- a/docs/libssh2_userauth_publickey_sk.md +++ b/docs/libssh2_userauth_publickey_sk.md @@ -128,13 +128,13 @@ to the callback. *sig_r* - For Ed25519 signatures, this contains the entire signature, as returned directly from the authenticator. For ECDSA signatures, this contains the r component of the signature in a big-endian binary representation. For -both algorithms, use LIBSSH2_ALLOC to allocate memory. It is freed by the +both algorithms, use SSH2_ALLOC to allocate memory. It is freed by the caller. *sig_r_len* - The length of the sig_r parameter. *sig_s* - For ECDSA signatures, this contains the s component of the -signature in a big-endian binary representation. Use LIBSSH2_ALLOC to allocate +signature in a big-endian binary representation. Use SSH2_ALLOC to allocate memory. It is freed by the caller. For Ed25519 signatures, set this to NULL. diff --git a/os400/ccsid.c b/os400/ccsid.c index 20c896f6..c8f34983 100644 --- a/os400/ccsid.c +++ b/os400/ccsid.c @@ -57,9 +57,9 @@ #define OFFSET_OF(t, f) ((size_t) ((char *) &((t *) 0)->f - (char *) 0)) -#define ALLOC(s, sz) ((s) ? LIBSSH2_ALLOC(s, sz) : malloc(sz)) -#define REALLOC(s, p, sz) ((s) ? LIBSSH2_REALLOC(s, p, sz) : realloc(p, sz)) -#define FREE(s, p) ((s) ? LIBSSH2_FREE(s, p) : free(p)) +#define ALLOC(s, sz) ((s) ? SSH2_ALLOC(s, sz) : malloc(sz)) +#define REALLOC(s, p, sz) ((s) ? SSH2_REALLOC(s, p, sz) : realloc(p, sz)) +#define FREE(s, p) ((s) ? SSH2_FREE(s, p) : free(p)) struct _libssh2_string_cache { libssh2_string_cache * next; diff --git a/os400/libssh2rpg/libssh2.rpgle.in b/os400/libssh2rpg/libssh2.rpgle.in index f7326125..f31cd664 100644 --- a/os400/libssh2rpg/libssh2.rpgle.in +++ b/os400/libssh2rpg/libssh2.rpgle.in @@ -177,15 +177,6 @@ d LIBSSH2_SSH_DEFAULT_BANNER... d c 'SSH-2.0-libssh2_@LIBSSH2_VERSION@' - * Default generate and safe prime sizes for - * diffie-hellman-group-exchange-sha1. - d LIBSSH2_DH_GEX_MINGROUP... - d c 1024 - d LIBSSH2_DH_GEX_OPTGROUP... - d c 1536 - d LIBSSH2_DH_GEX_MAXGROUP... - d c 2048 - * Defaults for pty requests. d LIBSSH2_TERM_WIDTH... d c 80 diff --git a/src/agent.c b/src/agent.c index 7619d16e..3324f9a3 100644 --- a/src/agent.c +++ b/src/agent.c @@ -60,16 +60,6 @@ #include "userauth.h" #include "session.h" -#if 0 -/* Requests from client to agent for protocol 1 key operations */ -#define SSH_AGENTC_REQUEST_RSA_IDENTITIES 1 -#define SSH_AGENTC_RSA_CHALLENGE 3 -#define SSH_AGENTC_ADD_RSA_IDENTITY 7 -#define SSH_AGENTC_REMOVE_RSA_IDENTITY 8 -#define SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES 9 -#define SSH_AGENTC_ADD_RSA_ID_CONSTRAINED 24 -#endif - /* Requests from client to agent for protocol 2 key operations */ #define SSH2_AGENTC_REQUEST_IDENTITIES 11 #define SSH2_AGENTC_SIGN_REQUEST 13 @@ -262,25 +252,19 @@ static int agent_connect_openssh(LIBSSH2_AGENT *agent) } for(;;) { - pipe = CreateFileA( - path, - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - /* Non-blocking mode for agent connections is not implemented at - * the point this was implemented. The code for Win32 OpenSSH - * should support non-blocking IO, but the code calling it does not - * support it as of yet. - * When non-blocking I/O is implemented for the surrounding code, - * uncomment the following line to enable support within the Win32 - * OpenSSH code. - */ - /* FILE_FLAG_OVERLAPPED | */ - SECURITY_SQOS_PRESENT | - SECURITY_IDENTIFICATION, - NULL - ); + /* Non-blocking mode for agent connections is not implemented at + * the point this was implemented. The code for Win32 OpenSSH + * should support non-blocking IO, but the code calling it does not + * support it as of yet. + * When non-blocking I/O is implemented for the surrounding code, + * uncomment the following line to enable support within the Win32 + * OpenSSH code. + */ + pipe = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, + OPEN_EXISTING, + /* FILE_FLAG_OVERLAPPED | */ + SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, + NULL); if(pipe != INVALID_HANDLE_VALUE) break; @@ -293,21 +277,21 @@ static int agent_connect_openssh(LIBSSH2_AGENT *agent) } if(pipe == INVALID_HANDLE_VALUE) { - ret = _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "unable to connect to agent pipe"); + ret = ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "unable to connect to agent pipe"); goto cleanup; } if(SetHandleInformation(pipe, HANDLE_FLAG_INHERIT, 0) == FALSE) { - ret = _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "unable to set handle information of agent pipe"); + ret = ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "unable to set handle information of agent pipe"); goto cleanup; } event = CreateEventA(NULL, TRUE, FALSE, NULL); if(!event) { - ret = _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "unable to create async I/O event"); + ret = ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "unable to create async I/O event"); goto cleanup; } @@ -382,14 +366,14 @@ static int agent_transact_openssh(LIBSSH2_AGENT *agent, /* Send the length of the request */ if(transctx->state == agent_NB_state_request_created) { - _libssh2_htonu32(buf, (uint32_t)transctx->request_len); + ssh2_htonu32(buf, (uint32_t)transctx->request_len); rc = win32_openssh_send_all(agent, buf, sizeof(buf), &transctx->send_recv_total); if(rc == LIBSSH2_ERROR_EAGAIN) return LIBSSH2_ERROR_EAGAIN; else if(rc < 0) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_SEND, - "agent send failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_SEND, + "agent send failed"); transctx->state = agent_NB_state_request_length_sent; } @@ -401,8 +385,8 @@ static int agent_transact_openssh(LIBSSH2_AGENT *agent, if(rc == LIBSSH2_ERROR_EAGAIN) return LIBSSH2_ERROR_EAGAIN; else if(rc < 0) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_SEND, - "agent send failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_SEND, + "agent send failed"); transctx->state = agent_NB_state_request_sent; } @@ -413,11 +397,11 @@ static int agent_transact_openssh(LIBSSH2_AGENT *agent, if(rc == LIBSSH2_ERROR_EAGAIN) return LIBSSH2_ERROR_EAGAIN; else if(rc < 0) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_RECV, - "agent recv failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_RECV, + "agent recv failed"); - transctx->response_len = _libssh2_ntohu32(buf); - transctx->response = LIBSSH2_ALLOC(agent->session, + transctx->response_len = ssh2_ntohu32(buf); + transctx->response = SSH2_ALLOC(agent->session, transctx->response_len); if(!transctx->response) return LIBSSH2_ERROR_ALLOC; @@ -433,8 +417,8 @@ static int agent_transact_openssh(LIBSSH2_AGENT *agent, if(rc == LIBSSH2_ERROR_EAGAIN) return LIBSSH2_ERROR_EAGAIN; else if(rc < 0) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_RECV, - "agent recv failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_RECV, + "agent recv failed"); transctx->state = agent_NB_state_response_received; } @@ -444,17 +428,17 @@ static int agent_transact_openssh(LIBSSH2_AGENT *agent, static int agent_disconnect_openssh(LIBSSH2_AGENT *agent) { if(!CancelIo(agent->pipe)) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "failed to cancel pending IO of agent pipe"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "failed to cancel pending IO of agent pipe"); if(!CloseHandle(agent->overlapped.hEvent)) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "failed to close handle to async I/O event"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "failed to close handle to async I/O event"); agent->overlapped.hEvent = NULL; /* let queued APCs (if any) drain */ SleepEx(0, TRUE); if(!CloseHandle(agent->pipe)) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "failed to close handle to agent pipe"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "failed to close handle to agent pipe"); agent->pipe = INVALID_HANDLE_VALUE; agent->fd = LIBSSH2_INVALID_SOCKET; @@ -481,20 +465,20 @@ static int agent_connect_unix(LIBSSH2_AGENT *agent) if(!path) { path = getenv("SSH_AUTH_SOCK"); if(!path) - return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE, - "no auth sock variable"); + return ssh2_err(agent->session, LIBSSH2_ERROR_BAD_USE, + "no auth sock variable"); } agent->fd = socket(PF_UNIX, SOCK_STREAM, 0); if(agent->fd < 0) - return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_SOCKET, - "failed creating socket"); + return ssh2_err(agent->session, LIBSSH2_ERROR_BAD_SOCKET, + "failed creating socket"); plen = strlen(path); if(plen >= sizeof(s_un.sun_path)) { close(agent->fd); - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "SSH_AUTH_SOCK path too long"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "SSH_AUTH_SOCK path too long"); } s_un.sun_family = AF_UNIX; @@ -503,8 +487,8 @@ static int agent_connect_unix(LIBSSH2_AGENT *agent) if(connect(agent->fd, (struct sockaddr *)(&s_un), sizeof(s_un)) != 0) { close(agent->fd); - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "failed connecting with agent"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "failed connecting with agent"); } return LIBSSH2_ERROR_NONE; @@ -532,8 +516,7 @@ static ssize_t _send_all(LIBSSH2_SEND_FUNC(func), libssh2_socket_t socket, const void *buffer, size_t length, int flags, void **abstract) { - RECV_SEND_ALL(func, socket, LIBSSH2_UNCONST(buffer), length, - flags, abstract); + RECV_SEND_ALL(func, socket, SSH2_UNCONST(buffer), length, flags, abstract); } static ssize_t _recv_all(LIBSSH2_RECV_FUNC(func), libssh2_socket_t socket, @@ -551,15 +534,15 @@ static int agent_transact_unix(LIBSSH2_AGENT *agent, /* Send the length of the request */ if(transctx->state == agent_NB_state_request_created) { - _libssh2_htonu32(buf, (uint32_t)transctx->request_len); + ssh2_htonu32(buf, (uint32_t)transctx->request_len); rc = (int)_send_all(agent->session->send, agent->fd, buf, sizeof(buf), 0, &agent->session->abstract); if(rc == -EAGAIN) return LIBSSH2_ERROR_EAGAIN; else if(rc < 0) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_SEND, - "agent send failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_SEND, + "agent send failed"); transctx->state = agent_NB_state_request_length_sent; } @@ -571,8 +554,8 @@ static int agent_transact_unix(LIBSSH2_AGENT *agent, if(rc == -EAGAIN) return LIBSSH2_ERROR_EAGAIN; else if(rc < 0) - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_SEND, - "agent send failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_SEND, + "agent send failed"); transctx->state = agent_NB_state_request_sent; } @@ -584,11 +567,11 @@ static int agent_transact_unix(LIBSSH2_AGENT *agent, if(rc < 0) { if(rc == -EAGAIN) return LIBSSH2_ERROR_EAGAIN; - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_RECV, - "agent recv failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_RECV, + "agent recv failed"); } - transctx->response_len = _libssh2_ntohu32(buf); - transctx->response = LIBSSH2_ALLOC(agent->session, + transctx->response_len = ssh2_ntohu32(buf); + transctx->response = SSH2_ALLOC(agent->session, transctx->response_len); if(!transctx->response) return LIBSSH2_ERROR_ALLOC; @@ -604,8 +587,8 @@ static int agent_transact_unix(LIBSSH2_AGENT *agent, if(rc < 0) { if(rc == -EAGAIN) return LIBSSH2_ERROR_EAGAIN; - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_RECV, - "agent recv failed"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_RECV, + "agent recv failed"); } transctx->state = agent_NB_state_response_received; } @@ -620,8 +603,8 @@ static int agent_disconnect_unix(LIBSSH2_AGENT *agent) if(ret != -1) agent->fd = LIBSSH2_INVALID_SOCKET; else - return _libssh2_error(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "failed closing the agent socket"); + return ssh2_err(agent->session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "failed closing the agent socket"); return LIBSSH2_ERROR_NONE; } @@ -648,8 +631,8 @@ static int agent_connect_pageant(LIBSSH2_AGENT *agent) HWND hwnd; hwnd = FindWindowA("Pageant", "Pageant"); if(!hwnd) - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "failed connecting agent"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "failed connecting agent"); agent->fd = 0; /* Mark as the connection has been established */ return LIBSSH2_ERROR_NONE; } @@ -666,13 +649,12 @@ static int agent_transact_pageant(LIBSSH2_AGENT *agent, COPYDATASTRUCT cds; if(!transctx || 4 + transctx->request_len > PAGEANT_MAX_MSGLEN) - return _libssh2_error(agent->session, LIBSSH2_ERROR_INVAL, - "illegal input"); + return ssh2_err(agent->session, LIBSSH2_ERROR_INVAL, "illegal input"); hwnd = FindWindowA("Pageant", "Pageant"); if(!hwnd) - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "found no pageant"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "found no pageant"); snprintf(mapname, sizeof(mapname), "PageantRequest%08x", (unsigned)GetCurrentThreadId()); @@ -680,18 +662,18 @@ static int agent_transact_pageant(LIBSSH2_AGENT *agent, 0, PAGEANT_MAX_MSGLEN, mapname); if(!filemap || filemap == INVALID_HANDLE_VALUE) - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "failed setting up pageant filemap"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "failed setting up pageant filemap"); p2 = p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0); if(!p || !p2) { CloseHandle(filemap); - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "failed to open pageant filemap for writing"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "failed to open pageant filemap for writing"); } - _libssh2_store_str(&p2, (const char *)transctx->request, - transctx->request_len); + ssh2_store_str(&p2, (const char *)transctx->request, + transctx->request_len); cds.dwData = PAGEANT_COPYDATA_ID; cds.cbData = (DWORD)(1 + strlen(mapname)); @@ -699,28 +681,28 @@ static int agent_transact_pageant(LIBSSH2_AGENT *agent, id = SendMessage(hwnd, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds); if(id > 0) { - transctx->response_len = _libssh2_ntohu32(p); + transctx->response_len = ssh2_ntohu32(p); if(transctx->response_len > PAGEANT_MAX_MSGLEN - 4) { UnmapViewOfFile(p); CloseHandle(filemap); - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "agent setup fail"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "agent setup fail"); } - transctx->response = LIBSSH2_ALLOC(agent->session, - transctx->response_len); + transctx->response = SSH2_ALLOC(agent->session, + transctx->response_len); if(!transctx->response) { UnmapViewOfFile(p); CloseHandle(filemap); - return _libssh2_error(agent->session, LIBSSH2_ERROR_ALLOC, - "agent malloc"); + return ssh2_err(agent->session, LIBSSH2_ERROR_ALLOC, + "agent malloc"); } memcpy(transctx->response, p + 4, transctx->response_len); } else { UnmapViewOfFile(p); CloseHandle(filemap); - return _libssh2_error(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, - "pageant did not handle message"); + return ssh2_err(agent->session, LIBSSH2_ERROR_AGENT_PROTOCOL, + "pageant did not handle message"); } UnmapViewOfFile(p); @@ -773,17 +755,16 @@ static int agent_sign(LIBSSH2_SESSION *session, /* Create a request to sign the data */ if(transctx->state == agent_NB_state_init) { - s = transctx->request = LIBSSH2_ALLOC(session, len); + s = transctx->request = SSH2_ALLOC(session, len); if(!transctx->request) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "out of memory"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, "out of memory"); *s++ = SSH2_AGENTC_SIGN_REQUEST; /* key blob */ - _libssh2_store_str(&s, (const char *)identity->external.blob, - identity->external.blob_len); + ssh2_store_str(&s, (const char *)identity->external.blob, + identity->external.blob_len); /* data */ - _libssh2_store_str(&s, (const char *)data, data_len); + ssh2_store_str(&s, (const char *)data, data_len); /* flags */ if(session->userauth_pblc_method_len > 0 && @@ -798,7 +779,7 @@ static int agent_sign(LIBSSH2_SESSION *session, sign_flags = SSH_AGENT_RSA_SHA2_256; } } - _libssh2_store_u32(&s, sign_flags); + ssh2_store_u32(&s, sign_flags); transctx->request_len = s - transctx->request; transctx->send_recv_total = 0; @@ -807,19 +788,17 @@ static int agent_sign(LIBSSH2_SESSION *session, /* Make sure to be re-called as a result of EAGAIN. */ if(*transctx->request != SSH2_AGENTC_SIGN_REQUEST) - return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE, - "illegal request"); + return ssh2_err(session, LIBSSH2_ERROR_BAD_USE, "illegal request"); if(!agent->ops) /* if no agent has been connected, bail out */ - return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE, - "agent not connected"); + return ssh2_err(session, LIBSSH2_ERROR_BAD_USE, "agent not connected"); rc = agent->ops->transact(agent, transctx); if(rc) { goto error; } - LIBSSH2_FREE(session, transctx->request); + SSH2_FREE(session, transctx->request); transctx->request = NULL; len = transctx->response_len; @@ -849,7 +828,7 @@ static int agent_sign(LIBSSH2_SESSION *session, rc = LIBSSH2_ERROR_AGENT_PROTOCOL; goto error; } - method_len = _libssh2_ntohu32(s); + method_len = ssh2_ntohu32(s); s += 4; len -= method_len; if(len < 0) { @@ -858,7 +837,7 @@ static int agent_sign(LIBSSH2_SESSION *session, } /* method name */ - method_name = LIBSSH2_ALLOC(session, method_len); + method_name = SSH2_ALLOC(session, method_len); if(!method_name) { rc = LIBSSH2_ERROR_ALLOC; goto error; @@ -873,8 +852,8 @@ static int agent_sign(LIBSSH2_SESSION *session, if(((size_t)method_len != session->userauth_pblc_method_len && method_len != plain_len) || memcmp(method_name, session->userauth_pblc_method, method_len)) { - _libssh2_debug((session, LIBSSH2_TRACE_KEX, "Agent sign method %.*s", - (int)method_len, method_name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agent sign method %.*s", + (int)method_len, method_name)); rc = LIBSSH2_ERROR_ALGO_UNSUPPORTED; goto error; @@ -886,7 +865,7 @@ static int agent_sign(LIBSSH2_SESSION *session, rc = LIBSSH2_ERROR_AGENT_PROTOCOL; goto error; } - *sig_len = _libssh2_ntohu32(s); + *sig_len = ssh2_ntohu32(s); s += 4; len -= *sig_len; if(len < 0) { @@ -894,7 +873,7 @@ static int agent_sign(LIBSSH2_SESSION *session, goto error; } - *sig = LIBSSH2_ALLOC(session, *sig_len); + *sig = SSH2_ALLOC(session, *sig_len); if(!*sig) { rc = LIBSSH2_ERROR_ALLOC; goto error; @@ -904,17 +883,17 @@ static int agent_sign(LIBSSH2_SESSION *session, error: if(method_name) - LIBSSH2_FREE(session, method_name); + SSH2_FREE(session, method_name); - LIBSSH2_FREE(session, transctx->request); + SSH2_FREE(session, transctx->request); transctx->request = NULL; - LIBSSH2_FREE(session, transctx->response); + SSH2_FREE(session, transctx->response); transctx->response = NULL; transctx->state = agent_NB_state_init; - return _libssh2_error(session, rc, "agent sign failure"); + return ssh2_err(session, rc, "agent sign failure"); } static int agent_list_identities(LIBSSH2_AGENT *agent) @@ -927,7 +906,7 @@ static int agent_list_identities(LIBSSH2_AGENT *agent) /* Create a request to list identities */ if(transctx->state == agent_NB_state_init) { - transctx->request = LIBSSH2_UNCONST(&c); + transctx->request = SSH2_UNCONST(&c); transctx->request_len = 1; transctx->send_recv_total = 0; transctx->state = agent_NB_state_request_created; @@ -935,17 +914,17 @@ static int agent_list_identities(LIBSSH2_AGENT *agent) /* Make sure to be re-called as a result of EAGAIN. */ if(*transctx->request != SSH2_AGENTC_REQUEST_IDENTITIES) - return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE, - "illegal agent request"); + return ssh2_err(agent->session, LIBSSH2_ERROR_BAD_USE, + "illegal agent request"); if(!agent->ops) /* if no agent has been connected, bail out */ - return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE, - "agent not connected"); + return ssh2_err(agent->session, LIBSSH2_ERROR_BAD_USE, + "agent not connected"); rc = agent->ops->transact(agent, transctx); if(rc) { - LIBSSH2_FREE(agent->session, transctx->response); + SSH2_FREE(agent->session, transctx->response); transctx->response = NULL; return rc; } @@ -970,7 +949,7 @@ static int agent_list_identities(LIBSSH2_AGENT *agent) rc = LIBSSH2_ERROR_AGENT_PROTOCOL; goto error; } - num_identities = _libssh2_ntohu32(s); + num_identities = ssh2_ntohu32(s); s += 4; while(num_identities--) { @@ -983,27 +962,27 @@ static int agent_list_identities(LIBSSH2_AGENT *agent) rc = LIBSSH2_ERROR_AGENT_PROTOCOL; goto error; } - identity = LIBSSH2_ALLOC(agent->session, sizeof(*identity)); + identity = SSH2_ALLOC(agent->session, sizeof(*identity)); if(!identity) { rc = LIBSSH2_ERROR_ALLOC; goto error; } - identity->external.blob_len = _libssh2_ntohu32(s); + identity->external.blob_len = ssh2_ntohu32(s); s += 4; /* Read the blob */ len -= identity->external.blob_len; if(len < 0) { rc = LIBSSH2_ERROR_AGENT_PROTOCOL; - LIBSSH2_FREE(agent->session, identity); + SSH2_FREE(agent->session, identity); goto error; } - identity->external.blob = LIBSSH2_ALLOC(agent->session, - identity->external.blob_len); + identity->external.blob = SSH2_ALLOC(agent->session, + identity->external.blob_len); if(!identity->external.blob) { rc = LIBSSH2_ERROR_ALLOC; - LIBSSH2_FREE(agent->session, identity); + SSH2_FREE(agent->session, identity); goto error; } memcpy(identity->external.blob, s, identity->external.blob_len); @@ -1013,41 +992,41 @@ static int agent_list_identities(LIBSSH2_AGENT *agent) len -= 4; if(len < 0) { rc = LIBSSH2_ERROR_AGENT_PROTOCOL; - LIBSSH2_FREE(agent->session, identity->external.blob); - LIBSSH2_FREE(agent->session, identity); + SSH2_FREE(agent->session, identity->external.blob); + SSH2_FREE(agent->session, identity); goto error; } - comment_len = _libssh2_ntohu32(s); + comment_len = ssh2_ntohu32(s); s += 4; if(comment_len > (size_t)len) { rc = LIBSSH2_ERROR_AGENT_PROTOCOL; - LIBSSH2_FREE(agent->session, identity->external.blob); - LIBSSH2_FREE(agent->session, identity); + SSH2_FREE(agent->session, identity->external.blob); + SSH2_FREE(agent->session, identity); goto error; } /* Read the comment */ len -= comment_len; - identity->external.comment = LIBSSH2_ALLOC(agent->session, - comment_len + 1); + identity->external.comment = SSH2_ALLOC(agent->session, + comment_len + 1); if(!identity->external.comment) { rc = LIBSSH2_ERROR_ALLOC; - LIBSSH2_FREE(agent->session, identity->external.blob); - LIBSSH2_FREE(agent->session, identity); + SSH2_FREE(agent->session, identity->external.blob); + SSH2_FREE(agent->session, identity); goto error; } identity->external.comment[comment_len] = '\0'; memcpy(identity->external.comment, s, comment_len); s += comment_len; - _libssh2_list_add(&agent->head, &identity->node); + ssh2_list_add(&agent->head, &identity->node); } error: - LIBSSH2_FREE(agent->session, transctx->response); + SSH2_FREE(agent->session, transctx->response); transctx->response = NULL; - return _libssh2_error(agent->session, rc, "agent list id failed"); + return ssh2_err(agent->session, rc, "agent list id failed"); } static void agent_free_identities(LIBSSH2_AGENT *agent) @@ -1055,13 +1034,13 @@ static void agent_free_identities(LIBSSH2_AGENT *agent) struct agent_publickey *node; struct agent_publickey *next; - for(node = _libssh2_list_first(&agent->head); node; node = next) { - next = _libssh2_list_next(&node->node); - LIBSSH2_FREE(agent->session, node->external.blob); - LIBSSH2_FREE(agent->session, node->external.comment); - LIBSSH2_FREE(agent->session, node); + for(node = ssh2_list_first(&agent->head); node; node = next) { + next = ssh2_list_next(&node->node); + SSH2_FREE(agent->session, node->external.blob); + SSH2_FREE(agent->session, node->external.comment); + SSH2_FREE(agent->session, node); } - _libssh2_list_init(&agent->head); + ssh2_list_init(&agent->head); } #define AGENT_PUBLICKEY_MAGIC 0x3bdefed2 @@ -1087,16 +1066,16 @@ LIBSSH2_AGENT *libssh2_agent_init(LIBSSH2_SESSION *session) { LIBSSH2_AGENT *agent; - agent = LIBSSH2_CALLOC(session, sizeof(*agent)); + agent = SSH2_CALLOC(session, sizeof(*agent)); if(!agent) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate space for agent connection"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate space for agent connection"); return NULL; } agent->fd = LIBSSH2_INVALID_SOCKET; agent->session = session; agent->identity_agent_path = NULL; - _libssh2_list_init(&agent->head); + ssh2_list_init(&agent->head); #ifdef HAVE_WIN32_AGENTS agent->pipe = INVALID_HANDLE_VALUE; @@ -1160,10 +1139,10 @@ int libssh2_agent_get_identity(LIBSSH2_AGENT *agent, struct agent_publickey *prev_node = prev->node; /* get the next node in the list */ - node = _libssh2_list_next(&prev_node->node); + node = ssh2_list_next(&prev_node->node); } else - node = _libssh2_list_first(&agent->head); + node = ssh2_list_first(&agent->head); if(!node) /* no (more) node */ @@ -1187,18 +1166,18 @@ int libssh2_agent_userauth(LIBSSH2_AGENT *agent, void *abstract = agent; int rc; - if(agent->session->userauth_pblc_state == libssh2_NB_state_idle) { + if(agent->session->userauth_pblc_state == ssh2_NB_state_idle) { memset(&agent->transctx, 0, sizeof(agent->transctx)); agent->identity = identity->node; } BLOCK_ADJUST(rc, agent->session, - _libssh2_userauth_publickey(agent->session, username, - strlen(username), - identity->blob, - identity->blob_len, - agent_sign, - &abstract)); + ssh2_userauth_publickey(agent->session, username, + strlen(username), + identity->blob, + identity->blob_len, + agent_sign, + &abstract)); return rc; } @@ -1221,7 +1200,7 @@ int libssh2_agent_sign(LIBSSH2_AGENT *agent, int rc; uint32_t key_kind_len; - if(agent->session->userauth_pblc_state == libssh2_NB_state_idle) { + if(agent->session->userauth_pblc_state == ssh2_NB_state_idle) { memset(&agent->transctx, 0, sizeof(agent->transctx)); agent->identity = identity->node; } @@ -1230,21 +1209,21 @@ int libssh2_agent_sign(LIBSSH2_AGENT *agent, return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } - key_kind_len = _libssh2_ntohu32(identity->blob); + key_kind_len = ssh2_ntohu32(identity->blob); if(identity->blob_len < sizeof(uint32_t) + key_kind_len) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } agent->session->userauth_pblc_method_len = method_len; - agent->session->userauth_pblc_method = LIBSSH2_ALLOC(agent->session, - method_len); + agent->session->userauth_pblc_method = SSH2_ALLOC(agent->session, + method_len); memcpy(agent->session->userauth_pblc_method, method, method_len); rc = agent_sign(agent->session, sig, s_len, data, d_len, &abstract); - LIBSSH2_FREE(agent->session, agent->session->userauth_pblc_method); + SSH2_FREE(agent->session, agent->session->userauth_pblc_method); agent->session->userauth_pblc_method = NULL; agent->session->userauth_pblc_method_len = 0; @@ -1277,10 +1256,10 @@ void libssh2_agent_free(LIBSSH2_AGENT *agent) } if(agent->identity_agent_path) - LIBSSH2_FREE(agent->session, agent->identity_agent_path); + SSH2_FREE(agent->session, agent->identity_agent_path); agent_free_identities(agent); - LIBSSH2_FREE(agent->session, agent); + SSH2_FREE(agent->session, agent); } /* @@ -1290,14 +1269,14 @@ LIBSSH2_API void libssh2_agent_set_identity_path(LIBSSH2_AGENT *agent, const char *path) { if(agent->identity_agent_path) { - LIBSSH2_FREE(agent->session, agent->identity_agent_path); + SSH2_FREE(agent->session, agent->identity_agent_path); agent->identity_agent_path = NULL; } if(path) { size_t path_len = strlen(path); if(path_len < SIZE_MAX - 1) { - char *path_buf = LIBSSH2_ALLOC(agent->session, path_len + 1); + char *path_buf = SSH2_ALLOC(agent->session, path_len + 1); memcpy(path_buf, path, path_len); path_buf[path_len] = '\0'; agent->identity_agent_path = path_buf; diff --git a/src/bcrypt_pbkdf.c b/src/bcrypt_pbkdf.c index 9435204a..13e9fbf0 100644 --- a/src/bcrypt_pbkdf.c +++ b/src/bcrypt_pbkdf.c @@ -88,9 +88,9 @@ static void bcrypt_hash(uint8_t *sha2pass, uint8_t *sha2salt, uint8_t *out) } /* zap */ - _libssh2_explicit_zero(ciphertext, sizeof(ciphertext)); - _libssh2_explicit_zero(cdata, sizeof(cdata)); - _libssh2_explicit_zero(&state, sizeof(state)); + ssh2_explicit_zero(ciphertext, sizeof(ciphertext)); + ssh2_explicit_zero(cdata, sizeof(cdata)); + ssh2_explicit_zero(&state, sizeof(state)); } static int bcrypt_pbkdf(const char *pass, size_t passlen, @@ -105,7 +105,7 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen, size_t i, j, amt, stride; uint32_t count; size_t origkeylen = keylen; - libssh2_sha512_ctx ctx; + ssh2_sha512_ctx ctx; /* nothing crazy */ if(rounds < 1) @@ -124,9 +124,9 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen, memcpy(countsalt, salt, saltlen); /* collapse password */ - if(!libssh2_sha512_init(&ctx) || - !libssh2_sha512_update(ctx, pass, passlen) || - !libssh2_sha512_final(ctx, sha2pass)) { + if(!ssh2_sha512_init(&ctx) || + !ssh2_sha512_update(ctx, pass, passlen) || + !ssh2_sha512_final(ctx, sha2pass)) { free(countsalt); return -1; } @@ -139,10 +139,10 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen, countsalt[saltlen + 3] = count & 0xff; /* first round, salt is salt */ - if(!libssh2_sha512_init(&ctx) || - !libssh2_sha512_update(ctx, countsalt, saltlen + 4) || - !libssh2_sha512_final(ctx, sha2salt)) { - _libssh2_explicit_zero(out, sizeof(out)); + if(!ssh2_sha512_init(&ctx) || + !ssh2_sha512_update(ctx, countsalt, saltlen + 4) || + !ssh2_sha512_final(ctx, sha2salt)) { + ssh2_explicit_zero(out, sizeof(out)); free(countsalt); return -1; } @@ -152,10 +152,10 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen, for(i = 1; i < rounds; i++) { /* subsequent rounds, salt is previous output */ - if(!libssh2_sha512_init(&ctx) || - !libssh2_sha512_update(ctx, tmpout, sizeof(tmpout)) || - !libssh2_sha512_final(ctx, sha2salt)) { - _libssh2_explicit_zero(out, sizeof(out)); + if(!ssh2_sha512_init(&ctx) || + !ssh2_sha512_update(ctx, tmpout, sizeof(tmpout)) || + !ssh2_sha512_final(ctx, sha2salt)) { + ssh2_explicit_zero(out, sizeof(out)); free(countsalt); return -1; } @@ -168,7 +168,7 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen, /* * pbkdf2 deviation: output the key material non-linearly. */ - amt = LIBSSH2_MIN(amt, keylen); + amt = SSH2_MIN(amt, keylen); for(i = 0; i < amt; i++) { size_t dest = i * stride + (count - 1); if(dest >= origkeylen) { @@ -180,7 +180,7 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen, } /* zap */ - _libssh2_explicit_zero(out, sizeof(out)); + ssh2_explicit_zero(out, sizeof(out)); free(countsalt); return 0; @@ -188,13 +188,13 @@ static int bcrypt_pbkdf(const char *pass, size_t passlen, /* Wrapper */ -int _libssh2_bcrypt_pbkdf(const char *pass, - size_t passlen, - const uint8_t *salt, - size_t saltlen, - uint8_t *key, - size_t keylen, - unsigned int rounds) +int ssh2_bcrypt_pbkdf(const char *pass, + size_t passlen, + const uint8_t *salt, + size_t saltlen, + uint8_t *key, + size_t keylen, + unsigned int rounds) { return bcrypt_pbkdf(pass, passlen, diff --git a/src/channel.c b/src/channel.c index d534ce88..8c749a6d 100644 --- a/src/channel.c +++ b/src/channel.c @@ -58,18 +58,18 @@ /* * Determine the next channel ID we can use at our end */ -uint32_t _libssh2_channel_nextid(LIBSSH2_SESSION *session) +uint32_t ssh2_channel_nextid(LIBSSH2_SESSION *session) { uint32_t id = session->next_channel; LIBSSH2_CHANNEL *channel; - channel = _libssh2_list_first(&session->channels); + channel = ssh2_list_first(&session->channels); while(channel) { if(channel->local.id > id) { id = channel->local.id; } - channel = _libssh2_list_next(&channel->node); + channel = ssh2_list_next(&channel->node); } /* This is a shortcut to avoid waiting for close packets on channels we @@ -80,23 +80,21 @@ uint32_t _libssh2_channel_nextid(LIBSSH2_SESSION *session) * told... */ session->next_channel = id + 1; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Allocated new channel ID#%u", id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, "Allocated new channel ID#%u", id)); return id; } /* * Locate a channel pointer by number */ -LIBSSH2_CHANNEL *_libssh2_channel_locate(LIBSSH2_SESSION *session, - uint32_t channel_id) +LIBSSH2_CHANNEL *ssh2_channel_locate(LIBSSH2_SESSION *session, + uint32_t channel_id) { LIBSSH2_CHANNEL *channel; LIBSSH2_LISTENER *l; - for(channel = _libssh2_list_first(&session->channels); - channel; - channel = _libssh2_list_next(&channel->node)) { + for(channel = ssh2_list_first(&session->channels); channel; + channel = ssh2_list_next(&channel->node)) { if(channel->local.id == channel_id) return channel; } @@ -104,11 +102,10 @@ LIBSSH2_CHANNEL *_libssh2_channel_locate(LIBSSH2_SESSION *session, /* We did not find the channel in the session, let us then check its listeners since each listener may have its own set of pending channels */ - for(l = _libssh2_list_first(&session->listeners); l; - l = _libssh2_list_next(&l->node)) { - for(channel = _libssh2_list_first(&l->queue); - channel; - channel = _libssh2_list_next(&channel->node)) { + for(l = ssh2_list_first(&session->listeners); l; + l = ssh2_list_next(&l->node)) { + for(channel = ssh2_list_first(&l->queue); channel; + channel = ssh2_list_next(&channel->node)) { if(channel->local.id == channel_id) return channel; } @@ -120,13 +117,13 @@ LIBSSH2_CHANNEL *_libssh2_channel_locate(LIBSSH2_SESSION *session, /* * Establish a generic session channel */ -LIBSSH2_CHANNEL *_libssh2_channel_open(LIBSSH2_SESSION *session, - const char *channel_type, - uint32_t channel_type_len, - uint32_t window_size, - uint32_t packet_size, - const unsigned char *message, - size_t message_len) +LIBSSH2_CHANNEL *ssh2_channel_open(LIBSSH2_SESSION *session, + const char *channel_type, + uint32_t channel_type_len, + uint32_t window_size, + uint32_t packet_size, + const unsigned char *message, + size_t message_len) { static const unsigned char reply_codes[3] = { SSH_MSG_CHANNEL_OPEN_CONFIRMATION, @@ -136,36 +133,35 @@ LIBSSH2_CHANNEL *_libssh2_channel_open(LIBSSH2_SESSION *session, unsigned char *s; int rc; - if(session->open_state == libssh2_NB_state_idle) { + if(session->open_state == ssh2_NB_state_idle) { session->open_channel = NULL; session->open_packet = NULL; session->open_data = NULL; /* 17 = packet_type(1) + channel_type_len(4) + sender_channel(4) + * window_size(4) + packet_size(4) */ session->open_packet_len = channel_type_len + 17; - session->open_local_channel = _libssh2_channel_nextid(session); + session->open_local_channel = ssh2_channel_nextid(session); /* Zero the whole thing out */ memset(&session->open_packet_requirev_state, 0, sizeof(session->open_packet_requirev_state)); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Opening Channel - win %d pack %d", window_size, - packet_size)); - session->open_channel = - LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Opening Channel - win %d pack %d", + window_size, packet_size)); + session->open_channel = SSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if(!session->open_channel) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate space for channel data"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate space for channel data"); return NULL; } session->open_channel->channel_type_len = channel_type_len; session->open_channel->channel_type = - LIBSSH2_ALLOC(session, channel_type_len); + SSH2_ALLOC(session, channel_type_len); if(!session->open_channel->channel_type) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed allocating memory for channel type name"); - LIBSSH2_FREE(session, session->open_channel); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed allocating memory for channel type name"); + SSH2_FREE(session, session->open_channel); session->open_channel = NULL; return NULL; } @@ -179,124 +175,120 @@ LIBSSH2_CHANNEL *_libssh2_channel_open(LIBSSH2_SESSION *session, session->open_channel->remote.packet_size = packet_size; session->open_channel->session = session; - _libssh2_list_add(&session->channels, &session->open_channel->node); + ssh2_list_add(&session->channels, &session->open_channel->node); s = session->open_packet = - LIBSSH2_ALLOC(session, session->open_packet_len); + SSH2_ALLOC(session, session->open_packet_len); if(!session->open_packet) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate temporary space for packet"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate temporary space for packet"); goto channel_error; } *(s++) = SSH_MSG_CHANNEL_OPEN; - _libssh2_store_str(&s, channel_type, channel_type_len); - _libssh2_store_u32(&s, session->open_local_channel); - _libssh2_store_u32(&s, window_size); - _libssh2_store_u32(&s, packet_size); + ssh2_store_str(&s, channel_type, channel_type_len); + ssh2_store_u32(&s, session->open_local_channel); + ssh2_store_u32(&s, window_size); + ssh2_store_u32(&s, packet_size); /* Do not copy the message */ - session->open_state = libssh2_NB_state_created; + session->open_state = ssh2_NB_state_created; } - if(session->open_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, - session->open_packet, - session->open_packet_len, - message, message_len); + if(session->open_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, + session->open_packet, + session->open_packet_len, + message, message_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, - "Would block sending channel-open request"); + ssh2_err(session, rc, "Would block sending channel-open request"); return NULL; } else if(rc) { - _libssh2_error(session, rc, "Unable to send channel-open request"); + ssh2_err(session, rc, "Unable to send channel-open request"); goto channel_error; } - session->open_state = libssh2_NB_state_sent; + session->open_state = ssh2_NB_state_sent; } - if(session->open_state == libssh2_NB_state_sent) { - rc = _libssh2_packet_requirev(session, reply_codes, - &session->open_data, - &session->open_data_len, 1, - session->open_packet + 5 + - channel_type_len, 4, - &session->open_packet_requirev_state); + if(session->open_state == ssh2_NB_state_sent) { + rc = ssh2_packet_requirev(session, reply_codes, + &session->open_data, + &session->open_data_len, 1, + session->open_packet + 5 + channel_type_len, + 4, + &session->open_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return NULL; } else if(rc) { - _libssh2_error(session, rc, "Unexpected error"); + ssh2_err(session, rc, "Unexpected error"); goto channel_error; } if(session->open_data_len < 1) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet size"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Unexpected packet size"); goto channel_error; } if(session->open_data[0] == SSH_MSG_CHANNEL_OPEN_CONFIRMATION) { if(session->open_data_len < 17) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet size"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected packet size"); goto channel_error; } session->open_channel->remote.id = - _libssh2_ntohu32(session->open_data + 5); + ssh2_ntohu32(session->open_data + 5); session->open_channel->local.window_size = - _libssh2_ntohu32(session->open_data + 9); + ssh2_ntohu32(session->open_data + 9); session->open_channel->local.window_size_initial = - _libssh2_ntohu32(session->open_data + 9); + ssh2_ntohu32(session->open_data + 9); session->open_channel->local.packet_size = - _libssh2_ntohu32(session->open_data + 13); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Connection Established - ID: %u/%u win: %u/%u" - " pack: %u/%u", - session->open_channel->local.id, - session->open_channel->remote.id, - session->open_channel->local.window_size, - session->open_channel->remote.window_size, - session->open_channel->local.packet_size, - session->open_channel->remote.packet_size)); - LIBSSH2_FREE(session, session->open_packet); + ssh2_ntohu32(session->open_data + 13); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Connection Established - ID: %u/%u win: %u/%u" + " pack: %u/%u", + session->open_channel->local.id, + session->open_channel->remote.id, + session->open_channel->local.window_size, + session->open_channel->remote.window_size, + session->open_channel->local.packet_size, + session->open_channel->remote.packet_size)); + SSH2_FREE(session, session->open_packet); session->open_packet = NULL; - LIBSSH2_FREE(session, session->open_data); + SSH2_FREE(session, session->open_data); session->open_data = NULL; - session->open_state = libssh2_NB_state_idle; + session->open_state = ssh2_NB_state_idle; return session->open_channel; } if(session->open_data[0] == SSH_MSG_CHANNEL_OPEN_FAILURE) { - unsigned int reason_code = - _libssh2_ntohu32(session->open_data + 5); + unsigned int reason_code = ssh2_ntohu32(session->open_data + 5); switch(reason_code) { case SSH_OPEN_ADMINISTRATIVELY_PROHIBITED: - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Channel open failure " - "(administratively prohibited)"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Channel open failure (administratively prohibited)"); break; case SSH_OPEN_CONNECT_FAILED: - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Channel open failure (connect failed)"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Channel open failure (connect failed)"); break; case SSH_OPEN_UNKNOWN_CHANNELTYPE: - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Channel open failure (unknown channel type)"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Channel open failure (unknown channel type)"); break; case SSH_OPEN_RESOURCE_SHORTAGE: - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Channel open failure (resource shortage)"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Channel open failure (resource shortage)"); break; default: - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Channel open failure"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Channel open failure"); } } } @@ -304,38 +296,36 @@ LIBSSH2_CHANNEL *_libssh2_channel_open(LIBSSH2_SESSION *session, channel_error: if(session->open_data) { - LIBSSH2_FREE(session, session->open_data); + SSH2_FREE(session, session->open_data); session->open_data = NULL; } if(session->open_packet) { - LIBSSH2_FREE(session, session->open_packet); + SSH2_FREE(session, session->open_packet); session->open_packet = NULL; } if(session->open_channel) { unsigned char channel_id[4]; - LIBSSH2_FREE(session, session->open_channel->channel_type); + SSH2_FREE(session, session->open_channel->channel_type); - _libssh2_list_remove(&session->open_channel->node); + ssh2_list_remove(&session->open_channel->node); /* Clear out packets meant for this channel */ - _libssh2_htonu32(channel_id, session->open_channel->local.id); - while((_libssh2_packet_ask(session, SSH_MSG_CHANNEL_DATA, - &session->open_data, - &session->open_data_len, 1, - channel_id, 4) >= 0) || - (_libssh2_packet_ask(session, SSH_MSG_CHANNEL_EXTENDED_DATA, - &session->open_data, - &session->open_data_len, 1, - channel_id, 4) >= 0)) { - LIBSSH2_FREE(session, session->open_data); + ssh2_htonu32(channel_id, session->open_channel->local.id); + while((ssh2_packet_ask(session, SSH_MSG_CHANNEL_DATA, + &session->open_data, &session->open_data_len, 1, + channel_id, 4) >= 0) || + (ssh2_packet_ask(session, SSH_MSG_CHANNEL_EXTENDED_DATA, + &session->open_data, &session->open_data_len, 1, + channel_id, 4) >= 0)) { + SSH2_FREE(session, session->open_data); session->open_data = NULL; } - LIBSSH2_FREE(session, session->open_channel); + SSH2_FREE(session, session->open_channel); session->open_channel = NULL; } - session->open_state = libssh2_NB_state_idle; + session->open_state = ssh2_NB_state_idle; return NULL; } @@ -357,11 +347,11 @@ LIBSSH2_CHANNEL *libssh2_channel_open_ex(LIBSSH2_SESSION *session, return NULL; BLOCK_ADJUST_ERRNO(ptr, session, - _libssh2_channel_open(session, - channel_type, channel_type_len, - window_size, packet_size, - (const unsigned char *)message, - message_len)); + ssh2_channel_open(session, + channel_type, channel_type_len, + window_size, packet_size, + (const unsigned char *)message, + message_len)); return ptr; } @@ -375,49 +365,48 @@ static LIBSSH2_CHANNEL *channel_direct_tcpip(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel; unsigned char *s; - if(session->direct_state == libssh2_NB_state_idle) { + if(session->direct_state == ssh2_NB_state_idle) { session->direct_host_len = strlen(host); session->direct_shost_len = strlen(shost); /* host_len(4) + port(4) + shost_len(4) + sport(4) */ session->direct_message_len = session->direct_host_len + session->direct_shost_len + 16; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Requesting direct-tcpip session from %s:%d to %s:%d", - shost, sport, host, port)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Requesting direct-tcpip session from %s:%d to %s:%d", + shost, sport, host, port)); s = session->direct_message = - LIBSSH2_ALLOC(session, session->direct_message_len); + SSH2_ALLOC(session, session->direct_message_len); if(!session->direct_message) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "direct-tcpip connection"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for direct-tcpip connection"); return NULL; } - _libssh2_store_str(&s, host, session->direct_host_len); - _libssh2_store_u32(&s, port); - _libssh2_store_str(&s, shost, session->direct_shost_len); - _libssh2_store_u32(&s, sport); + ssh2_store_str(&s, host, session->direct_host_len); + ssh2_store_u32(&s, port); + ssh2_store_str(&s, shost, session->direct_shost_len); + ssh2_store_u32(&s, sport); } - channel = _libssh2_channel_open(session, "direct-tcpip", - sizeof("direct-tcpip") - 1, - LIBSSH2_CHANNEL_WINDOW_DEFAULT, - LIBSSH2_CHANNEL_PACKET_DEFAULT, - session->direct_message, - session->direct_message_len); + channel = ssh2_channel_open(session, "direct-tcpip", + sizeof("direct-tcpip") - 1, + LIBSSH2_CHANNEL_WINDOW_DEFAULT, + LIBSSH2_CHANNEL_PACKET_DEFAULT, + session->direct_message, + session->direct_message_len); if(!channel && libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) { /* The error code is still set to LIBSSH2_ERROR_EAGAIN, set our state to created to avoid re-creating the package on next invoke */ - session->direct_state = libssh2_NB_state_created; + session->direct_state = ssh2_NB_state_created; return NULL; } /* by default we set (keep?) idle state... */ - session->direct_state = libssh2_NB_state_idle; + session->direct_state = ssh2_NB_state_idle; - LIBSSH2_FREE(session, session->direct_message); + SSH2_FREE(session, session->direct_message); session->direct_message = NULL; return channel; @@ -453,48 +442,46 @@ static LIBSSH2_CHANNEL *channel_direct_streamlocal(LIBSSH2_SESSION *session, LIBSSH2_CHANNEL *channel; unsigned char *s; - if(session->direct_state == libssh2_NB_state_idle) { + if(session->direct_state == ssh2_NB_state_idle) { session->direct_host_len = strlen(socket_path); session->direct_shost_len = strlen(shost); session->direct_message_len = session->direct_host_len + session->direct_shost_len + 12; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Requesting direct-streamlocal session to %s", - socket_path)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Requesting direct-streamlocal session to %s", socket_path)); s = session->direct_message = - LIBSSH2_ALLOC(session, session->direct_message_len); + SSH2_ALLOC(session, session->direct_message_len); if(!session->direct_message) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory " - "for direct-streamlocal connection"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory " + "for direct-streamlocal connection"); return NULL; } - _libssh2_store_str(&s, socket_path, session->direct_host_len); - _libssh2_store_str(&s, shost, session->direct_shost_len); - _libssh2_store_u32(&s, sport); + ssh2_store_str(&s, socket_path, session->direct_host_len); + ssh2_store_str(&s, shost, session->direct_shost_len); + ssh2_store_u32(&s, sport); } - channel = - _libssh2_channel_open(session, "direct-streamlocal@openssh.com", - sizeof("direct-streamlocal@openssh.com") - 1, - LIBSSH2_CHANNEL_WINDOW_DEFAULT, - LIBSSH2_CHANNEL_PACKET_DEFAULT, - session->direct_message, - session->direct_message_len); + channel = ssh2_channel_open(session, "direct-streamlocal@openssh.com", + sizeof("direct-streamlocal@openssh.com") - 1, + LIBSSH2_CHANNEL_WINDOW_DEFAULT, + LIBSSH2_CHANNEL_PACKET_DEFAULT, + session->direct_message, + session->direct_message_len); if(!channel && libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) { /* The error code is still set to LIBSSH2_ERROR_EAGAIN, set our state to created to avoid re-creating the package on next invoke */ - session->direct_state = libssh2_NB_state_created; + session->direct_state = ssh2_NB_state_created; return NULL; } /* by default we set (keep?) idle state... */ - session->direct_state = libssh2_NB_state_idle; + session->direct_state = ssh2_NB_state_idle; - LIBSSH2_FREE(session, session->direct_message); + SSH2_FREE(session, session->direct_message); session->direct_message = NULL; return channel; @@ -535,7 +522,7 @@ static LIBSSH2_LISTENER *channel_forward_listen(LIBSSH2_SESSION *session, if(!host) host = "0.0.0.0"; - if(session->fwdLstn_state == libssh2_NB_state_idle) { + if(session->fwdLstn_state == ssh2_NB_state_idle) { session->fwdLstn_host_len = (uint32_t)strlen(host); /* 14 = packet_type(1) + request_len(4) + want_replay(1) + host_len(4) + port(4) */ @@ -547,85 +534,83 @@ static LIBSSH2_LISTENER *channel_forward_listen(LIBSSH2_SESSION *session, memset(&session->fwdLstn_packet_requirev_state, 0, sizeof(session->fwdLstn_packet_requirev_state)); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Requesting tcpip-forward session for %s:%d", host, - port)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Requesting tcpip-forward session for %s:%d", host, port)); s = session->fwdLstn_packet = - LIBSSH2_ALLOC(session, session->fwdLstn_packet_len); + SSH2_ALLOC(session, session->fwdLstn_packet_len); if(!session->fwdLstn_packet) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for setenv packet"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for setenv packet"); return NULL; } *(s++) = SSH_MSG_GLOBAL_REQUEST; - _libssh2_store_str(&s, "tcpip-forward", sizeof("tcpip-forward") - 1); + ssh2_store_str(&s, "tcpip-forward", sizeof("tcpip-forward") - 1); *(s++) = 0x01; /* want_reply */ - _libssh2_store_str(&s, host, session->fwdLstn_host_len); - _libssh2_store_u32(&s, port); + ssh2_store_str(&s, host, session->fwdLstn_host_len); + ssh2_store_u32(&s, port); - session->fwdLstn_state = libssh2_NB_state_created; + session->fwdLstn_state = ssh2_NB_state_created; } - if(session->fwdLstn_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, - session->fwdLstn_packet, - session->fwdLstn_packet_len, - NULL, 0); + if(session->fwdLstn_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, + session->fwdLstn_packet, + session->fwdLstn_packet_len, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block sending global-request packet for " - "forward listen request"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block sending global-request packet for " + "forward listen request"); return NULL; } else if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send global-request packet for forward " - "listen request"); - LIBSSH2_FREE(session, session->fwdLstn_packet); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send global-request packet for forward " + "listen request"); + SSH2_FREE(session, session->fwdLstn_packet); session->fwdLstn_packet = NULL; - session->fwdLstn_state = libssh2_NB_state_idle; + session->fwdLstn_state = ssh2_NB_state_idle; return NULL; } - LIBSSH2_FREE(session, session->fwdLstn_packet); + SSH2_FREE(session, session->fwdLstn_packet); session->fwdLstn_packet = NULL; - session->fwdLstn_state = libssh2_NB_state_sent; + session->fwdLstn_state = ssh2_NB_state_sent; } - if(session->fwdLstn_state == libssh2_NB_state_sent) { + if(session->fwdLstn_state == ssh2_NB_state_sent) { unsigned char *data; size_t data_len; - rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, - 0, NULL, 0, - &session->fwdLstn_packet_requirev_state); + rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len, + 0, NULL, 0, + &session->fwdLstn_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, "Would block"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); return NULL; } else if(rc || (data_len < 1)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Unknown"); - session->fwdLstn_state = libssh2_NB_state_idle; + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Unknown"); + session->fwdLstn_state = ssh2_NB_state_idle; return NULL; } if(data[0] == SSH_MSG_REQUEST_SUCCESS) { LIBSSH2_LISTENER *listener; - listener = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_LISTENER)); + listener = SSH2_CALLOC(session, sizeof(LIBSSH2_LISTENER)); if(!listener) - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for listener queue"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for listener queue"); else { listener->host = - LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1); + SSH2_ALLOC(session, session->fwdLstn_host_len + 1); if(!listener->host) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory " - "for listener queue"); - LIBSSH2_FREE(session, listener); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for listener queue"); + SSH2_FREE(session, listener); listener = NULL; } else { @@ -633,10 +618,10 @@ static LIBSSH2_LISTENER *channel_forward_listen(LIBSSH2_SESSION *session, memcpy(listener->host, host, session->fwdLstn_host_len); listener->host[session->fwdLstn_host_len] = 0; if(data_len >= 5 && !port) { - listener->port = _libssh2_ntohu32(data + 1); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Dynamic tcpip-forward port " - "allocated: %d", listener->port)); + listener->port = ssh2_ntohu32(data + 1); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Dynamic tcpip-forward port allocated: %d", + listener->port)); } else listener->port = port; @@ -645,7 +630,7 @@ static LIBSSH2_LISTENER *channel_forward_listen(LIBSSH2_SESSION *session, listener->queue_maxsize = queue_maxsize; /* append this to the parent's list of listeners */ - _libssh2_list_add(&session->listeners, &listener->node); + ssh2_list_add(&session->listeners, &listener->node); if(bound_port) { *bound_port = listener->port; @@ -653,20 +638,20 @@ static LIBSSH2_LISTENER *channel_forward_listen(LIBSSH2_SESSION *session, } } - LIBSSH2_FREE(session, data); - session->fwdLstn_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->fwdLstn_state = ssh2_NB_state_idle; return listener; } else if(data[0] == SSH_MSG_REQUEST_FAILURE) { - LIBSSH2_FREE(session, data); - _libssh2_error(session, LIBSSH2_ERROR_REQUEST_DENIED, - "Unable to complete request for forward-listen"); - session->fwdLstn_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_REQUEST_DENIED, + "Unable to complete request for forward-listen"); + session->fwdLstn_state = ssh2_NB_state_idle; return NULL; } } - session->fwdLstn_state = libssh2_NB_state_idle; + session->fwdLstn_state = ssh2_NB_state_idle; return NULL; } @@ -697,7 +682,7 @@ LIBSSH2_LISTENER *libssh2_channel_forward_listen_ex(LIBSSH2_SESSION *session, * * Return 0 on success, LIBSSH2_ERROR_EAGAIN if would block, -1 on error */ -int _libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener) +int ssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener) { LIBSSH2_SESSION *session = listener->session; LIBSSH2_CHANNEL *queued; @@ -709,70 +694,70 @@ int _libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener) int rc; int retcode = 0; - if(listener->chanFwdCncl_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Cancelling tcpip-forward session for %s:%d", - listener->host, listener->port)); + if(listener->chanFwdCncl_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Cancelling tcpip-forward session for %s:%d", + listener->host, listener->port)); - s = packet = LIBSSH2_ALLOC(session, packet_len); + s = packet = SSH2_ALLOC(session, packet_len); if(!packet) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for setenv packet"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for setenv packet"); return LIBSSH2_ERROR_ALLOC; } *(s++) = SSH_MSG_GLOBAL_REQUEST; - _libssh2_store_str(&s, "cancel-tcpip-forward", - sizeof("cancel-tcpip-forward") - 1); + ssh2_store_str(&s, "cancel-tcpip-forward", + sizeof("cancel-tcpip-forward") - 1); *(s++) = 0x00; /* want_reply */ - _libssh2_store_str(&s, listener->host, host_len); - _libssh2_store_u32(&s, listener->port); + ssh2_store_str(&s, listener->host, host_len); + ssh2_store_u32(&s, listener->port); - listener->chanFwdCncl_state = libssh2_NB_state_created; + listener->chanFwdCncl_state = ssh2_NB_state_created; } else { packet = listener->chanFwdCncl_data; } - if(listener->chanFwdCncl_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, packet, packet_len, NULL, 0); + if(listener->chanFwdCncl_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, packet, packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, "Would block sending forward request"); + ssh2_err(session, rc, "Would block sending forward request"); listener->chanFwdCncl_data = packet; return rc; } else if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send global-request packet for forward " - "listen request"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send global-request packet for forward " + "listen request"); /* set the state to something we do not check for, for the unfortunate situation where we get an EAGAIN further down when trying to bail out due to errors! */ - listener->chanFwdCncl_state = libssh2_NB_state_sent; + listener->chanFwdCncl_state = ssh2_NB_state_sent; retcode = LIBSSH2_ERROR_SOCKET_SEND; } - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); - listener->chanFwdCncl_state = libssh2_NB_state_sent; + listener->chanFwdCncl_state = ssh2_NB_state_sent; } - queued = _libssh2_list_first(&listener->queue); + queued = ssh2_list_first(&listener->queue); while(queued) { - LIBSSH2_CHANNEL *next = _libssh2_list_next(&queued->node); + LIBSSH2_CHANNEL *next = ssh2_list_next(&queued->node); - rc = _libssh2_channel_free(queued); + rc = ssh2_channel_free(queued); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } queued = next; } - LIBSSH2_FREE(session, listener->host); + SSH2_FREE(session, listener->host); /* remove this entry from the parent's list of listeners */ - _libssh2_list_remove(&listener->node); + ssh2_list_remove(&listener->node); - LIBSSH2_FREE(session, listener); + SSH2_FREE(session, listener); return retcode; } @@ -791,8 +776,7 @@ int libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener) if(!listener) return LIBSSH2_ERROR_BAD_USE; - BLOCK_ADJUST(rc, listener->session, - _libssh2_channel_forward_cancel(listener)); + BLOCK_ADJUST(rc, listener->session, ssh2_channel_forward_cancel(listener)); return rc; } @@ -804,30 +788,30 @@ static LIBSSH2_CHANNEL *channel_forward_accept(LIBSSH2_LISTENER *listener) int rc; do { - rc = _libssh2_transport_read(listener->session); + rc = ssh2_transport_read(listener->session); } while(rc > 0); - if(_libssh2_list_first(&listener->queue)) { - LIBSSH2_CHANNEL *channel = _libssh2_list_first(&listener->queue); + if(ssh2_list_first(&listener->queue)) { + LIBSSH2_CHANNEL *channel = ssh2_list_first(&listener->queue); /* detach channel from listener's queue */ - _libssh2_list_remove(&channel->node); + ssh2_list_remove(&channel->node); listener->queue_size--; /* add channel to session's channel list */ - _libssh2_list_add(&channel->session->channels, &channel->node); + ssh2_list_add(&channel->session->channels, &channel->node); return channel; } if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(listener->session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for packet"); + ssh2_err(listener->session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for packet"); } else - _libssh2_error(listener->session, LIBSSH2_ERROR_CHANNEL_UNKNOWN, - "Channel not found"); + ssh2_err(listener->session, LIBSSH2_ERROR_CHANNEL_UNKNOWN, + "Channel not found"); return NULL; } @@ -861,7 +845,7 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel, size_t data_len; int rc; - if(channel->setenv_state == libssh2_NB_state_idle) { + if(channel->setenv_state == ssh2_NB_state_idle) { /* 21 = packet_type(1) + channel_id(4) + request_len(4) + * request(3)"env" + want_reply(1) + varname_len(4) + value_len(4) */ channel->setenv_packet_len = varname_len + value_len + 21; @@ -870,86 +854,82 @@ static int channel_setenv(LIBSSH2_CHANNEL *channel, memset(&channel->setenv_packet_requirev_state, 0, sizeof(channel->setenv_packet_requirev_state)); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Setting remote environment variable: %s=%s on " - "channel %u/%u", varname, value, channel->local.id, - channel->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, "Setting remote " + "environment variable: %s=%s on channel %u/%u", + varname, value, channel->local.id, channel->remote.id)); s = channel->setenv_packet = - LIBSSH2_ALLOC(session, channel->setenv_packet_len); + SSH2_ALLOC(session, channel->setenv_packet_len); if(!channel->setenv_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory " - "for setenv packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for setenv packet"); } *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_store_u32(&s, channel->remote.id); - _libssh2_store_str(&s, "env", sizeof("env") - 1); + ssh2_store_u32(&s, channel->remote.id); + ssh2_store_str(&s, "env", sizeof("env") - 1); *(s++) = 0x01; - _libssh2_store_str(&s, varname, varname_len); - _libssh2_store_str(&s, value, value_len); + ssh2_store_str(&s, varname, varname_len); + ssh2_store_str(&s, value, value_len); - channel->setenv_state = libssh2_NB_state_created; + channel->setenv_state = ssh2_NB_state_created; } - if(channel->setenv_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, - channel->setenv_packet, - channel->setenv_packet_len, - NULL, 0); + if(channel->setenv_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, + channel->setenv_packet, + channel->setenv_packet_len, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, - "Would block sending setenv request"); + ssh2_err(session, rc, "Would block sending setenv request"); return rc; } else if(rc) { - LIBSSH2_FREE(session, channel->setenv_packet); + SSH2_FREE(session, channel->setenv_packet); channel->setenv_packet = NULL; - channel->setenv_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send channel-request packet for " - "setenv request"); + channel->setenv_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send channel-request packet for " + "setenv request"); } - LIBSSH2_FREE(session, channel->setenv_packet); + SSH2_FREE(session, channel->setenv_packet); channel->setenv_packet = NULL; - _libssh2_htonu32(channel->setenv_local_channel, channel->local.id); + ssh2_htonu32(channel->setenv_local_channel, channel->local.id); - channel->setenv_state = libssh2_NB_state_sent; + channel->setenv_state = ssh2_NB_state_sent; } - if(channel->setenv_state == libssh2_NB_state_sent) { - rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, - 1, channel->setenv_local_channel, 4, - &channel->setenv_packet_requirev_state); + if(channel->setenv_state == ssh2_NB_state_sent) { + rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len, + 1, channel->setenv_local_channel, 4, + &channel->setenv_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if(rc) { - channel->setenv_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Failed getting response for " - "channel-setenv"); + channel->setenv_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, + "Failed getting response for channel-setenv"); } else if(data_len < 1) { - channel->setenv_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet size"); + channel->setenv_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected packet size"); } if(data[0] == SSH_MSG_CHANNEL_SUCCESS) { - LIBSSH2_FREE(session, data); - channel->setenv_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + channel->setenv_state = ssh2_NB_state_idle; return 0; } - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - channel->setenv_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, - "Unable to complete request for channel-setenv"); + channel->setenv_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, + "Unable to complete request for channel-setenv"); } /* @@ -986,13 +966,13 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel, { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; int rc; - if(channel->reqPTY_state == libssh2_NB_state_idle) { + if(channel->reqPTY_state == ssh2_NB_state_idle) { /* 41 = packet_type(1) + channel(4) + pty_req_len(4) + "pty_req"(7) + * want_reply(1) + term_len(4) + width(4) + height(4) + width_px(4) + * height_px(4) + modes_len(4) */ if(term_len + modes_len > 256) { - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "term + mode lengths too large"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "term + mode lengths too large"); } channel->reqPTY_packet_len = term_len + modes_len + 41; @@ -1001,73 +981,71 @@ static int channel_request_pty(LIBSSH2_CHANNEL *channel, memset(&channel->reqPTY_packet_requirev_state, 0, sizeof(channel->reqPTY_packet_requirev_state)); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Allocating tty on channel %u/%u", channel->local.id, - channel->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Allocating tty on channel %u/%u", + channel->local.id, channel->remote.id)); s = channel->reqPTY_packet; *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_store_u32(&s, channel->remote.id); - _libssh2_store_str(&s, (const char *)"pty-req", sizeof("pty-req") - 1); + ssh2_store_u32(&s, channel->remote.id); + ssh2_store_str(&s, (const char *)"pty-req", sizeof("pty-req") - 1); *(s++) = 0x01; - _libssh2_store_str(&s, term, term_len); - _libssh2_store_u32(&s, width); - _libssh2_store_u32(&s, height); - _libssh2_store_u32(&s, width_px); - _libssh2_store_u32(&s, height_px); - _libssh2_store_str(&s, modes, modes_len); + ssh2_store_str(&s, term, term_len); + ssh2_store_u32(&s, width); + ssh2_store_u32(&s, height); + ssh2_store_u32(&s, width_px); + ssh2_store_u32(&s, height_px); + ssh2_store_str(&s, modes, modes_len); - channel->reqPTY_state = libssh2_NB_state_created; + channel->reqPTY_state = ssh2_NB_state_created; } - if(channel->reqPTY_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, channel->reqPTY_packet, - channel->reqPTY_packet_len, NULL, 0); + if(channel->reqPTY_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, channel->reqPTY_packet, + channel->reqPTY_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, "Would block sending pty request"); + ssh2_err(session, rc, "Would block sending pty request"); return rc; } else if(rc) { - channel->reqPTY_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Unable to send pty-request packet"); + channel->reqPTY_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send pty-request packet"); } - _libssh2_htonu32(channel->reqPTY_local_channel, channel->local.id); + ssh2_htonu32(channel->reqPTY_local_channel, channel->local.id); - channel->reqPTY_state = libssh2_NB_state_sent; + channel->reqPTY_state = ssh2_NB_state_sent; } - if(channel->reqPTY_state == libssh2_NB_state_sent) { + if(channel->reqPTY_state == ssh2_NB_state_sent) { unsigned char *data; size_t data_len; unsigned char code; - rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, - 1, channel->reqPTY_local_channel, 4, - &channel->reqPTY_packet_requirev_state); + rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len, + 1, channel->reqPTY_local_channel, 4, + &channel->reqPTY_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc || data_len < 1) { - channel->reqPTY_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Failed to require the PTY package"); + channel->reqPTY_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Failed to require the PTY package"); } code = data[0]; - LIBSSH2_FREE(session, data); - channel->reqPTY_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + channel->reqPTY_state = ssh2_NB_state_idle; if(code == SSH_MSG_CHANNEL_SUCCESS) return 0; } - return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, - "Unable to complete request for " - "channel request-pty"); + return ssh2_err(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, + "Unable to complete request for channel request-pty"); } /* @@ -1083,13 +1061,13 @@ static int channel_request_auth_agent(LIBSSH2_CHANNEL *channel, { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; int rc; - if(channel->req_auth_agent_state == libssh2_NB_state_idle) { + if(channel->req_auth_agent_state == ssh2_NB_state_idle) { /* Only valid options are "auth-agent-req" and * "auth-agent-req_at_openssh.com" so we make sure it is not * actually longer than the longest possible. */ if(request_str_len > 26) { - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "request_str length too large"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "request_str length too large"); } /* @@ -1101,9 +1079,9 @@ static int channel_request_auth_agent(LIBSSH2_CHANNEL *channel, memset(&channel->req_auth_agent_requirev_state, 0, sizeof(channel->req_auth_agent_requirev_state)); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Requesting auth agent on channel %u/%u", - channel->local.id, channel->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Requesting auth agent on channel %u/%u", + channel->local.id, channel->remote.id)); /* * byte SSH_MSG_CHANNEL_REQUEST @@ -1113,64 +1091,59 @@ static int channel_request_auth_agent(LIBSSH2_CHANNEL *channel, * */ s = channel->req_auth_agent_packet; *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_store_u32(&s, channel->remote.id); - _libssh2_store_str(&s, (const char *)request_str, request_str_len); + ssh2_store_u32(&s, channel->remote.id); + ssh2_store_str(&s, (const char *)request_str, request_str_len); *(s++) = 0x01; - channel->req_auth_agent_state = libssh2_NB_state_created; + channel->req_auth_agent_state = ssh2_NB_state_created; } - if(channel->req_auth_agent_state == libssh2_NB_state_created) { + if(channel->req_auth_agent_state == ssh2_NB_state_created) { /* Send the packet, we can use sizeof() on the packet because it * is always completely filled; there are no variable length fields. */ - rc = _libssh2_transport_send(session, channel->req_auth_agent_packet, - channel->req_auth_agent_packet_len, - NULL, 0); + rc = ssh2_transport_send(session, channel->req_auth_agent_packet, + channel->req_auth_agent_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, - "Would block sending auth-agent request"); + ssh2_err(session, rc, "Would block sending auth-agent request"); return rc; } else if(rc) { - channel->req_auth_agent_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Unable to send auth-agent request"); + channel->req_auth_agent_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send auth-agent request"); } - _libssh2_htonu32(channel->req_auth_agent_local_channel, - channel->local.id); - channel->req_auth_agent_state = libssh2_NB_state_sent; + ssh2_htonu32(channel->req_auth_agent_local_channel, channel->local.id); + channel->req_auth_agent_state = ssh2_NB_state_sent; } - if(channel->req_auth_agent_state == libssh2_NB_state_sent) { + if(channel->req_auth_agent_state == ssh2_NB_state_sent) { unsigned char *data; size_t data_len; unsigned char code; - rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, - 1, channel->req_auth_agent_local_channel, - 4, - &channel->req_auth_agent_requirev_state); + rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len, + 1, channel->req_auth_agent_local_channel, 4, + &channel->req_auth_agent_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - channel->req_auth_agent_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Failed to request auth-agent"); + channel->req_auth_agent_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Failed to request auth-agent"); } code = data[0]; - LIBSSH2_FREE(session, data); - channel->req_auth_agent_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + channel->req_auth_agent_state = ssh2_NB_state_idle; if(code == SSH_MSG_CHANNEL_SUCCESS) return 0; } - return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, - "Unable to complete request for auth-agent"); + return ssh2_err(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, + "Unable to complete request for auth-agent"); } /* @@ -1193,7 +1166,7 @@ int libssh2_channel_request_auth_agent(LIBSSH2_CHANNEL *channel) * send "auth-agent-req," but most SSH servers out there right now * actually expect "auth-agent-req@openssh.com", so we try that * first. */ - if(channel->req_auth_agent_try_state == libssh2_NB_state_idle) { + if(channel->req_auth_agent_try_state == ssh2_NB_state_idle) { BLOCK_ADJUST(rc, channel->session, channel_request_auth_agent(channel, "auth-agent-req@openssh.com", @@ -1203,10 +1176,10 @@ int libssh2_channel_request_auth_agent(LIBSSH2_CHANNEL *channel) * the next step to try another request type. */ if(rc != LIBSSH2_ERROR_NONE && rc != LIBSSH2_ERROR_EAGAIN) - channel->req_auth_agent_try_state = libssh2_NB_state_sent; + channel->req_auth_agent_try_state = ssh2_NB_state_sent; } - if(channel->req_auth_agent_try_state == libssh2_NB_state_sent) { + if(channel->req_auth_agent_try_state == ssh2_NB_state_sent) { BLOCK_ADJUST(rc, channel->session, channel_request_auth_agent(channel, "auth-agent-req", 14)); @@ -1215,12 +1188,12 @@ int libssh2_channel_request_auth_agent(LIBSSH2_CHANNEL *channel) * state machine. */ if(rc != LIBSSH2_ERROR_NONE && rc != LIBSSH2_ERROR_EAGAIN) - channel->req_auth_agent_try_state = libssh2_NB_state_sent1; + channel->req_auth_agent_try_state = ssh2_NB_state_sent1; } /* If things are good, reset the try state. */ if(rc == LIBSSH2_ERROR_NONE) - channel->req_auth_agent_try_state = libssh2_NB_state_idle; + channel->req_auth_agent_try_state = ssh2_NB_state_idle; return rc; } @@ -1255,50 +1228,49 @@ static int channel_request_pty_size(LIBSSH2_CHANNEL *channel, int width, int rc; int retcode = LIBSSH2_ERROR_PROTO; - if(channel->reqPTY_state == libssh2_NB_state_idle) { + if(channel->reqPTY_state == ssh2_NB_state_idle) { channel->reqPTY_packet_len = 39; /* Zero the whole thing out */ memset(&channel->reqPTY_packet_requirev_state, 0, sizeof(channel->reqPTY_packet_requirev_state)); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "changing tty size on channel %u/%u", - channel->local.id, channel->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "changing tty size on channel %u/%u", + channel->local.id, channel->remote.id)); s = channel->reqPTY_packet; *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_store_u32(&s, channel->remote.id); - _libssh2_store_str(&s, (const char *)"window-change", - sizeof("window-change") - 1); + ssh2_store_u32(&s, channel->remote.id); + ssh2_store_str(&s, (const char *)"window-change", + sizeof("window-change") - 1); *(s++) = 0x00; /* Do not reply */ - _libssh2_store_u32(&s, width); - _libssh2_store_u32(&s, height); - _libssh2_store_u32(&s, width_px); - _libssh2_store_u32(&s, height_px); + ssh2_store_u32(&s, width); + ssh2_store_u32(&s, height); + ssh2_store_u32(&s, width_px); + ssh2_store_u32(&s, height_px); - channel->reqPTY_state = libssh2_NB_state_created; + channel->reqPTY_state = ssh2_NB_state_created; } - if(channel->reqPTY_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, channel->reqPTY_packet, - channel->reqPTY_packet_len, NULL, 0); + if(channel->reqPTY_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, channel->reqPTY_packet, + channel->reqPTY_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, - "Would block sending window-change request"); + ssh2_err(session, rc, "Would block sending window-change request"); return rc; } else if(rc) { - channel->reqPTY_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Unable to send window-change packet"); + channel->reqPTY_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, + "Unable to send window-change packet"); } - _libssh2_htonu32(channel->reqPTY_local_channel, channel->local.id); + ssh2_htonu32(channel->reqPTY_local_channel, channel->local.id); retcode = LIBSSH2_ERROR_NONE; } - channel->reqPTY_state = libssh2_NB_state_idle; + channel->reqPTY_state = ssh2_NB_state_idle; return retcode; } @@ -1338,7 +1310,7 @@ static int channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection, auth_cookie ? strlen(auth_cookie) : LIBSSH2_X11_RANDOM_COOKIE_LEN; int rc; - if(channel->reqX11_state == libssh2_NB_state_idle) { + if(channel->reqX11_state == ssh2_NB_state_idle) { /* 30 = packet_type(1) + channel(4) + x11_req_len(4) + "x11-req"(7) + * want_reply(1) + single_cnx(1) + proto_len(4) + cookie_len(4) + * screen_num(4) */ @@ -1348,33 +1320,31 @@ static int channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection, memset(&channel->reqX11_packet_requirev_state, 0, sizeof(channel->reqX11_packet_requirev_state)); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Requesting x11-req for channel %u/%u: single=%d " - "proto=%s cookie=%s screen=%d", - channel->local.id, channel->remote.id, - single_connection, - auth_proto ? auth_proto : "MIT-MAGIC-COOKIE-1", - auth_cookie ? auth_cookie : "", - screen_number)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Requesting x11-req for channel %u/%u: single=%d " + "proto=%s cookie=%s screen=%d", + channel->local.id, channel->remote.id, single_connection, + auth_proto ? auth_proto : "MIT-MAGIC-COOKIE-1", + auth_cookie ? auth_cookie : "", screen_number)); s = channel->reqX11_packet = - LIBSSH2_ALLOC(session, channel->reqX11_packet_len); + SSH2_ALLOC(session, channel->reqX11_packet_len); if(!channel->reqX11_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for pty-request"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for pty-request"); } *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_store_u32(&s, channel->remote.id); - _libssh2_store_str(&s, "x11-req", sizeof("x11-req") - 1); + ssh2_store_u32(&s, channel->remote.id); + ssh2_store_str(&s, "x11-req", sizeof("x11-req") - 1); *(s++) = 0x01; /* want_reply */ *(s++) = single_connection ? 0x01 : 0x00; - _libssh2_store_str(&s, auth_proto ? auth_proto : "MIT-MAGIC-COOKIE-1", - proto_len); + ssh2_store_str(&s, auth_proto ? auth_proto : "MIT-MAGIC-COOKIE-1", + proto_len); - _libssh2_store_u32(&s, (uint32_t)cookie_len); + ssh2_store_u32(&s, (uint32_t)cookie_len); if(auth_cookie) { /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */ memcpy(s, auth_cookie, cookie_len); @@ -1387,10 +1357,10 @@ static int channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection, 4 bytes of size (for screen_number) */ unsigned char buffer[LIBSSH2_X11_RANDOM_COOKIE_LEN / 2]; - if(_libssh2_random(buffer, LIBSSH2_X11_RANDOM_COOKIE_LEN / 2)) { - return _libssh2_error(session, LIBSSH2_ERROR_RANDGEN, - "Unable to get random bytes " - "for x11-req cookie"); + if(ssh2_random(buffer, LIBSSH2_X11_RANDOM_COOKIE_LEN / 2)) { + return ssh2_err(session, LIBSSH2_ERROR_RANDGEN, + "Unable to get random bytes " + "for x11-req cookie"); } for(i = 0; i < (LIBSSH2_X11_RANDOM_COOKIE_LEN / 2); i++) { snprintf((char *)&s[i * 2], 3, "%02X", buffer[i]); @@ -1398,59 +1368,58 @@ static int channel_x11_req(LIBSSH2_CHANNEL *channel, int single_connection, } s += cookie_len; - _libssh2_store_u32(&s, screen_number); - channel->reqX11_state = libssh2_NB_state_created; + ssh2_store_u32(&s, screen_number); + channel->reqX11_state = ssh2_NB_state_created; } - if(channel->reqX11_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, channel->reqX11_packet, - channel->reqX11_packet_len, NULL, 0); + if(channel->reqX11_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, channel->reqX11_packet, + channel->reqX11_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, "Would block sending X11-req packet"); + ssh2_err(session, rc, "Would block sending X11-req packet"); return rc; } if(rc) { - LIBSSH2_FREE(session, channel->reqX11_packet); + SSH2_FREE(session, channel->reqX11_packet); channel->reqX11_packet = NULL; - channel->reqX11_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Unable to send x11-req packet"); + channel->reqX11_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send x11-req packet"); } - LIBSSH2_FREE(session, channel->reqX11_packet); + SSH2_FREE(session, channel->reqX11_packet); channel->reqX11_packet = NULL; - _libssh2_htonu32(channel->reqX11_local_channel, channel->local.id); + ssh2_htonu32(channel->reqX11_local_channel, channel->local.id); - channel->reqX11_state = libssh2_NB_state_sent; + channel->reqX11_state = ssh2_NB_state_sent; } - if(channel->reqX11_state == libssh2_NB_state_sent) { + if(channel->reqX11_state == ssh2_NB_state_sent) { size_t data_len; unsigned char *data; unsigned char code; - rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, - 1, channel->reqX11_local_channel, 4, - &channel->reqX11_packet_requirev_state); + rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len, + 1, channel->reqX11_local_channel, 4, + &channel->reqX11_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc || data_len < 1) { - channel->reqX11_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "waiting for x11-req response packet"); + channel->reqX11_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, + "waiting for x11-req response packet"); } code = data[0]; - LIBSSH2_FREE(session, data); - channel->reqX11_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + channel->reqX11_state = ssh2_NB_state_idle; if(code == SSH_MSG_CHANNEL_SUCCESS) return 0; } - return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, - "Unable to complete request for channel x11-req"); + return ssh2_err(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, + "Unable to complete request for channel x11-req"); } /* @@ -1475,9 +1444,9 @@ int libssh2_channel_x11_req_ex(LIBSSH2_CHANNEL *channel, int single_connection, /* * Primitive for libssh2_channel_(shell|exec|subsystem) */ -int _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, - const char *request, size_t request_len, - const char *message, size_t message_len) +int ssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, + const char *request, size_t request_len, + const char *message, size_t message_len) { LIBSSH2_SESSION *session = channel->session; unsigned char *s; @@ -1485,12 +1454,12 @@ int _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, { SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 }; int rc; - if(channel->process_state == libssh2_NB_state_end) { - return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE, - "Channel can not be reused"); + if(channel->process_state == ssh2_NB_state_end) { + return ssh2_err(session, LIBSSH2_ERROR_BAD_USE, + "Channel can not be reused"); } - if(channel->process_state == libssh2_NB_state_idle) { + if(channel->process_state == ssh2_NB_state_idle) { /* 10 = packet_type(1) + channel(4) + request_len(4) + want_reply(1) */ channel->process_packet_len = request_len + 10; @@ -1501,80 +1470,78 @@ int _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, if(message) channel->process_packet_len += 4; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "starting request(%s) on channel %u/%u, message=%s", - request, channel->local.id, channel->remote.id, - message ? message : "")); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "starting request(%s) on channel %u/%u, message=%s", + request, channel->local.id, channel->remote.id, + message ? message : "")); s = channel->process_packet = - LIBSSH2_ALLOC(session, channel->process_packet_len); + SSH2_ALLOC(session, channel->process_packet_len); if(!channel->process_packet) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory " - "for channel-process request"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory " + "for channel-process request"); *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_store_u32(&s, channel->remote.id); - _libssh2_store_str(&s, request, request_len); + ssh2_store_u32(&s, channel->remote.id); + ssh2_store_str(&s, request, request_len); *(s++) = 0x01; if(message) - _libssh2_store_u32(&s, (uint32_t)message_len); + ssh2_store_u32(&s, (uint32_t)message_len); - channel->process_state = libssh2_NB_state_created; + channel->process_state = ssh2_NB_state_created; } - if(channel->process_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, - channel->process_packet, - channel->process_packet_len, - (const unsigned char *)message, - message_len); + if(channel->process_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, + channel->process_packet, + channel->process_packet_len, + (const unsigned char *)message, + message_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, "Would block sending channel request"); + ssh2_err(session, rc, "Would block sending channel request"); return rc; } else if(rc) { - LIBSSH2_FREE(session, channel->process_packet); + SSH2_FREE(session, channel->process_packet); channel->process_packet = NULL; - channel->process_state = libssh2_NB_state_end; - return _libssh2_error(session, rc, - "Unable to send channel request"); + channel->process_state = ssh2_NB_state_end; + return ssh2_err(session, rc, "Unable to send channel request"); } - LIBSSH2_FREE(session, channel->process_packet); + SSH2_FREE(session, channel->process_packet); channel->process_packet = NULL; - _libssh2_htonu32(channel->process_local_channel, channel->local.id); + ssh2_htonu32(channel->process_local_channel, channel->local.id); - channel->process_state = libssh2_NB_state_sent; + channel->process_state = ssh2_NB_state_sent; } - if(channel->process_state == libssh2_NB_state_sent) { + if(channel->process_state == ssh2_NB_state_sent) { unsigned char *data; size_t data_len; unsigned char code; - rc = _libssh2_packet_requirev(session, reply_codes, &data, &data_len, - 1, channel->process_local_channel, 4, - &channel->process_packet_requirev_state); + rc = ssh2_packet_requirev(session, reply_codes, &data, &data_len, + 1, channel->process_local_channel, 4, + &channel->process_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc || data_len < 1) { - channel->process_state = libssh2_NB_state_end; - return _libssh2_error(session, rc, - "Failed waiting for channel success"); + channel->process_state = ssh2_NB_state_end; + return ssh2_err(session, rc, "Failed waiting for channel success"); } code = data[0]; - LIBSSH2_FREE(session, data); - channel->process_state = libssh2_NB_state_end; + SSH2_FREE(session, data); + channel->process_state = ssh2_NB_state_end; if(code == SSH_MSG_CHANNEL_SUCCESS) return 0; } - return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, - "Unable to complete request for " - "channel-process-startup"); + return ssh2_err(session, LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED, + "Unable to complete request for " + "channel-process-startup"); } /* @@ -1593,9 +1560,9 @@ int libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, return LIBSSH2_ERROR_BAD_USE; BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_process_startup(channel, - request, request_len, - message, message_len)); + ssh2_channel_process_startup(channel, + request, request_len, + message, message_len)); return rc; } @@ -1607,29 +1574,28 @@ LIBSSH2_API void libssh2_channel_set_blocking(LIBSSH2_CHANNEL *channel, int blocking) { if(channel) - (void)_libssh2_session_set_blocking(channel->session, blocking); + (void)ssh2_session_set_blocking(channel->session, blocking); } /* * Flush data from one (or all) stream * Returns number of bytes flushed, or negative on failure */ -int _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid) +int ssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid) { - if(channel->flush_state == libssh2_NB_state_idle) { - struct packet *packet = - _libssh2_list_first(&channel->session->packets); + if(channel->flush_state == ssh2_NB_state_idle) { + struct packet *packet = ssh2_list_first(&channel->session->packets); channel->flush_refund_bytes = 0; channel->flush_flush_bytes = 0; while(packet) { unsigned char packet_type; - struct packet *next = _libssh2_list_next(&packet->node); + struct packet *next = ssh2_list_next(&packet->node); if(packet->data_len < 1) { packet = next; - _libssh2_debug((channel->session, LIBSSH2_TRACE_ERROR, - "Unexpected packet length")); + ssh2_deb((channel->session, LIBSSH2_TRACE_ERROR, + "Unexpected packet length")); continue; } @@ -1638,7 +1604,7 @@ int _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid) if(((packet_type == SSH_MSG_CHANNEL_DATA) || (packet_type == SSH_MSG_CHANNEL_EXTENDED_DATA)) && ((packet->data_len >= 5) && - (_libssh2_ntohu32(packet->data + 1) == channel->local.id))) { + (ssh2_ntohu32(packet->data + 1) == channel->local.id))) { /* It is our channel at least */ int packet_stream_id; @@ -1646,13 +1612,12 @@ int _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid) packet_stream_id = 0; } else if(packet->data_len >= 9) { - packet_stream_id = _libssh2_ntohu32(packet->data + 5); + packet_stream_id = ssh2_ntohu32(packet->data + 5); } else { - channel->flush_state = libssh2_NB_state_idle; - return _libssh2_error(channel->session, - LIBSSH2_ERROR_PROTO, - "Unexpected packet length"); + channel->flush_state = ssh2_NB_state_idle; + return ssh2_err(channel->session, LIBSSH2_ERROR_PROTO, + "Unexpected packet length"); } if((streamid == LIBSSH2_CHANNEL_FLUSH_ALL) || @@ -1664,42 +1629,40 @@ int _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid) size_t bytes_to_flush = packet->data_len - packet->data_head; - _libssh2_debug((channel->session, LIBSSH2_TRACE_CONN, - "Flushing %ld bytes of data from stream " - "%d on channel %u/%u", - (long)bytes_to_flush, packet_stream_id, - channel->local.id, channel->remote.id)); + ssh2_deb((channel->session, LIBSSH2_TRACE_CONN, + "Flushing %ld bytes of data from stream " + "%d on channel %u/%u", + (long)bytes_to_flush, packet_stream_id, + channel->local.id, channel->remote.id)); /* It is one of the streams we wanted to flush */ channel->flush_refund_bytes += packet->data_len - 13; channel->flush_flush_bytes += bytes_to_flush; - LIBSSH2_FREE(channel->session, packet->data); + SSH2_FREE(channel->session, packet->data); /* remove this packet from the parent's list */ - _libssh2_list_remove(&packet->node); - LIBSSH2_FREE(channel->session, packet); + ssh2_list_remove(&packet->node); + SSH2_FREE(channel->session, packet); } } packet = next; } - channel->flush_state = libssh2_NB_state_created; + channel->flush_state = ssh2_NB_state_created; } channel->read_avail -= channel->flush_flush_bytes; channel->remote.window_size -= (uint32_t)channel->flush_flush_bytes; if(channel->flush_refund_bytes) { - int rc = - _libssh2_channel_receive_window_adjust(channel, - (uint32_t)channel->flush_refund_bytes, - 1, NULL); + int rc = ssh2_channel_receive_window_adjust(channel, + (uint32_t)channel->flush_refund_bytes, 1, NULL); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; } - channel->flush_state = libssh2_NB_state_idle; + channel->flush_state = ssh2_NB_state_idle; return (int)channel->flush_flush_bytes; } @@ -1716,8 +1679,7 @@ int libssh2_channel_flush_ex(LIBSSH2_CHANNEL *channel, int streamid) if(!channel) return LIBSSH2_ERROR_BAD_USE; - BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_flush(channel, streamid)); + BLOCK_ADJUST(rc, channel->session, ssh2_channel_flush(channel, streamid)); return rc; } @@ -1761,11 +1723,11 @@ int libssh2_channel_get_exit_signal(LIBSSH2_CHANNEL *channel, if(channel->exit_signal) { namelen = strlen(channel->exit_signal); if(exitsignal) { - *exitsignal = LIBSSH2_ALLOC(session, namelen + 1); + *exitsignal = SSH2_ALLOC(session, namelen + 1); if(!*exitsignal) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory " - "for signal name"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory " + "for signal name"); } memcpy(*exitsignal, channel->exit_signal, namelen); (*exitsignal)[namelen] = '\0'; @@ -1803,26 +1765,25 @@ int libssh2_channel_get_exit_signal(LIBSSH2_CHANNEL *channel, * to be adjusted is less than LIBSSH2_CHANNEL_MINADJUST and force is 0 the * adjustment amount is queued for a later packet. * - * Calls _libssh2_error() ! + * Calls ssh2_err() ! */ -int _libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel, - uint32_t adjustment, - unsigned char force, - unsigned int *store) +int ssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel, + uint32_t adjustment, + unsigned char force, + unsigned int *store) { int rc; if(store) *store = channel->remote.window_size; - if(channel->adjust_state == libssh2_NB_state_idle) { + if(channel->adjust_state == ssh2_NB_state_idle) { if(!force && (adjustment + channel->adjust_queue < LIBSSH2_CHANNEL_MINADJUST)) { - _libssh2_debug((channel->session, LIBSSH2_TRACE_CONN, - "Queueing %u bytes for receive window adjustment " - "for channel %u/%u", - adjustment, channel->local.id, - channel->remote.id)); + ssh2_deb((channel->session, LIBSSH2_TRACE_CONN, + "Queueing %u bytes for receive window adjustment " + "for channel %u/%u", + adjustment, channel->local.id, channel->remote.id)); channel->adjust_queue += adjustment; return 0; } @@ -1836,40 +1797,38 @@ int _libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel, /* Adjust the window based on the block we freed */ channel->adjust_adjust[0] = SSH_MSG_CHANNEL_WINDOW_ADJUST; - _libssh2_htonu32(&channel->adjust_adjust[1], channel->remote.id); - _libssh2_htonu32(&channel->adjust_adjust[5], adjustment); - _libssh2_debug((channel->session, LIBSSH2_TRACE_CONN, - "Adjusting window %u bytes for data on " - "channel %u/%u", - adjustment, channel->local.id, channel->remote.id)); + ssh2_htonu32(&channel->adjust_adjust[1], channel->remote.id); + ssh2_htonu32(&channel->adjust_adjust[5], adjustment); + ssh2_deb((channel->session, LIBSSH2_TRACE_CONN, + "Adjusting window %u bytes for data on channel %u/%u", + adjustment, channel->local.id, channel->remote.id)); - channel->adjust_state = libssh2_NB_state_created; + channel->adjust_state = ssh2_NB_state_created; } - rc = _libssh2_transport_send(channel->session, channel->adjust_adjust, 9, - NULL, 0); + rc = ssh2_transport_send(channel->session, channel->adjust_adjust, 9, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(channel->session, rc, - "Would block sending window adjust"); + ssh2_err(channel->session, rc, "Would block sending window adjust"); return rc; } else if(rc) { channel->adjust_queue = adjustment; - return _libssh2_error(channel->session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send transfer-window adjustment " - "packet, deferring"); + return ssh2_err(channel->session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send transfer-window adjustment " + "packet, deferring"); } else { if(adjustment > UINT32_MAX - channel->remote.window_size) { - return _libssh2_error(channel->session, LIBSSH2_ERROR_PROTO, - "Window adjust out of bounds"); + return ssh2_err(channel->session, LIBSSH2_ERROR_PROTO, + "Window adjust out of bounds"); } else { channel->remote.window_size += adjustment; } } - channel->adjust_state = libssh2_NB_state_idle; + channel->adjust_state = ssh2_NB_state_idle; return 0; } @@ -1898,9 +1857,9 @@ unsigned long libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel, return (unsigned long)LIBSSH2_ERROR_BAD_USE; BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_receive_window_adjust(channel, - (uint32_t)adjustment, - force, &window)); + ssh2_channel_receive_window_adjust(channel, + (uint32_t)adjustment, + force, &window)); /* stupid - but this is how it was made to work before and this is kept for backwards compatibility */ @@ -1929,35 +1888,33 @@ int libssh2_channel_receive_window_adjust2(LIBSSH2_CHANNEL *channel, return LIBSSH2_ERROR_BAD_USE; BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_receive_window_adjust(channel, - (uint32_t)adjustment, - force, storewindow)); + ssh2_channel_receive_window_adjust(channel, + (uint32_t)adjustment, + force, storewindow)); return rc; } -int _libssh2_channel_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode) +int ssh2_channel_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode) { - if(channel->extData2_state == libssh2_NB_state_idle) { - _libssh2_debug((channel->session, LIBSSH2_TRACE_CONN, - "Setting channel %u/%u handle_extended_data" - " mode to %d", - channel->local.id, channel->remote.id, ignore_mode)); + if(channel->extData2_state == ssh2_NB_state_idle) { + ssh2_deb((channel->session, LIBSSH2_TRACE_CONN, + "Setting channel %u/%u handle_extended_data mode to %d", + channel->local.id, channel->remote.id, ignore_mode)); channel->remote.extended_data_ignore_mode = (char)ignore_mode; - channel->extData2_state = libssh2_NB_state_created; + channel->extData2_state = ssh2_NB_state_created; } - if(channel->extData2_state == libssh2_NB_state_idle) { + if(channel->extData2_state == ssh2_NB_state_idle) { if(ignore_mode == LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE) { - int rc = - _libssh2_channel_flush(channel, - LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA); + int rc = ssh2_channel_flush(channel, + LIBSSH2_CHANNEL_FLUSH_EXTENDED_DATA); if(LIBSSH2_ERROR_EAGAIN == rc) return rc; } } - channel->extData2_state = libssh2_NB_state_idle; + channel->extData2_state = ssh2_NB_state_idle; return 0; } @@ -1971,7 +1928,7 @@ int libssh2_channel_handle_extended_data2(LIBSSH2_CHANNEL *channel, return LIBSSH2_ERROR_BAD_USE; BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_extended_data(channel, ignore_mode)); + ssh2_channel_extended_data(channel, ignore_mode)); return rc; } @@ -2002,8 +1959,8 @@ void libssh2_channel_handle_extended_data(LIBSSH2_CHANNEL *channel, * The receive window must be maintained (enlarged) by the user of this * function. */ -ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, - char *buf, size_t buflen) +ssize_t ssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, + char *buf, size_t buflen) { LIBSSH2_SESSION *session = channel->session; int rc; @@ -2013,14 +1970,12 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, struct packet *read_packet; struct packet *read_next; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "channel_read() wants %ld bytes from channel %u/%u " - "stream #%d", - (long)buflen, channel->local.id, channel->remote.id, - stream_id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "channel_read() wants %ld bytes from channel %u/%u stream #%d", + (long)buflen, channel->local.id, channel->remote.id, stream_id)); /* expand the receiving window first if it has become too narrow */ - if((channel->read_state == libssh2_NB_state_jump1) || + if((channel->read_state == ssh2_NB_state_jump1) || (channel->remote.window_size < channel->remote.window_size_initial / 4 * 3 + buflen)) { @@ -2031,25 +1986,24 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, /* the actual window adjusting may not finish so we need to deal with this special state here */ - channel->read_state = libssh2_NB_state_jump1; - rc = _libssh2_channel_receive_window_adjust(channel, adjustment, - 0, NULL); + channel->read_state = ssh2_NB_state_jump1; + rc = ssh2_channel_receive_window_adjust(channel, adjustment, 0, NULL); if(rc) return rc; - channel->read_state = libssh2_NB_state_idle; + channel->read_state = ssh2_NB_state_idle; } /* Process all pending incoming packets. Tests prove that this way produces faster transfers. */ do { - rc = _libssh2_transport_read(session); + rc = ssh2_transport_read(session); } while(rc > 0); if((rc < 0) && (rc != LIBSSH2_ERROR_EAGAIN)) - return _libssh2_error(session, rc, "transport read"); + return ssh2_err(session, rc, "transport read"); - read_packet = _libssh2_list_first(&session->packets); + read_packet = ssh2_list_first(&session->packets); while(read_packet && (bytes_read < buflen)) { /* previously this loop condition also checked for !channel->remote.close but we cannot let it do this: @@ -2062,21 +2016,21 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, struct packet *readpkt = read_packet; /* In case packet gets destroyed during this iteration */ - read_next = _libssh2_list_next(&readpkt->node); + read_next = ssh2_list_next(&readpkt->node); if(readpkt->data_len < 5) { read_packet = read_next; if(readpkt->data_len != 1 || readpkt->data[0] != SSH_MSG_REQUEST_FAILURE) { - _libssh2_debug((channel->session, LIBSSH2_TRACE_ERROR, - "Unexpected packet length")); + ssh2_deb((channel->session, LIBSSH2_TRACE_ERROR, + "Unexpected packet length")); } continue; } - channel->read_local_id = _libssh2_ntohu32(readpkt->data + 1); + channel->read_local_id = ssh2_ntohu32(readpkt->data + 1); /* * Either we asked for a specific extended data stream @@ -2089,7 +2043,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, (readpkt->data[0] == SSH_MSG_CHANNEL_EXTENDED_DATA) && (channel->local.id == channel->read_local_id) && (readpkt->data_len >= 9) && - (stream_id == (int)_libssh2_ntohu32(readpkt->data + 5))) || + (stream_id == (int)ssh2_ntohu32(readpkt->data + 5))) || (!stream_id && (readpkt->data[0] == SSH_MSG_CHANNEL_DATA) && (channel->local.id == channel->read_local_id)) || (!stream_id && @@ -2109,11 +2063,11 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, unlink_packet = TRUE; } - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "channel_read() got %ld of data from %u/%u/%d%s", - (long)bytes_want, channel->local.id, - channel->remote.id, stream_id, - unlink_packet ? " [ul]" : "")); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "channel_read() got %ld of data from %u/%u/%d%s", + (long)bytes_want, channel->local.id, + channel->remote.id, stream_id, + unlink_packet ? " [ul]" : "")); /* copy data from this struct to the target buffer */ memcpy(&buf[bytes_read], @@ -2126,10 +2080,10 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, /* if drained, remove from list */ if(unlink_packet) { /* detach readpkt from session->packets list */ - _libssh2_list_remove(&readpkt->node); + ssh2_list_remove(&readpkt->node); - LIBSSH2_FREE(session, readpkt->data); - LIBSSH2_FREE(session, readpkt); + SSH2_FREE(session, readpkt->data); + SSH2_FREE(session, readpkt); } } @@ -2148,7 +2102,7 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, return 0; /* if the transport layer said EAGAIN then we say so as well */ - return _libssh2_error(session, rc, "would block"); + return ssh2_err(session, rc, "would block"); } channel->read_avail -= bytes_read; @@ -2183,13 +2137,13 @@ ssize_t libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, int stream_id, if(buflen > recv_window) { BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_receive_window_adjust(channel, - (uint32_t)buflen, - 1, NULL)); + ssh2_channel_receive_window_adjust(channel, + (uint32_t)buflen, + 1, NULL)); } BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_read(channel, stream_id, buf, buflen)); + ssh2_channel_read(channel, stream_id, buf, buflen)); return rc; } @@ -2197,30 +2151,29 @@ ssize_t libssh2_channel_read_ex(LIBSSH2_CHANNEL *channel, int stream_id, * Return the size of the data block of the current packet, or 0 if there * is not a packet. */ -size_t _libssh2_channel_packet_data_len(LIBSSH2_CHANNEL *channel, - int stream_id) +size_t ssh2_channel_packet_data_len(LIBSSH2_CHANNEL *channel, int stream_id) { LIBSSH2_SESSION *session = channel->session; struct packet *read_packet; struct packet *next_packet; uint32_t read_local_id; - read_packet = _libssh2_list_first(&session->packets); + read_packet = ssh2_list_first(&session->packets); if(!read_packet) return 0; while(read_packet) { - next_packet = _libssh2_list_next(&read_packet->node); + next_packet = ssh2_list_next(&read_packet->node); if(read_packet->data_len < 5) { read_packet = next_packet; - _libssh2_debug((channel->session, LIBSSH2_TRACE_ERROR, - "Unexpected packet length")); + ssh2_deb((channel->session, LIBSSH2_TRACE_ERROR, + "Unexpected packet length")); continue; } - read_local_id = _libssh2_ntohu32(read_packet->data + 1); + read_local_id = ssh2_ntohu32(read_packet->data + 1); /* * Either we asked for a specific extended data stream @@ -2233,7 +2186,7 @@ size_t _libssh2_channel_packet_data_len(LIBSSH2_CHANNEL *channel, (read_packet->data[0] == SSH_MSG_CHANNEL_EXTENDED_DATA) && (channel->local.id == read_local_id) && (read_packet->data_len >= 9) && - (stream_id == (int)_libssh2_ntohu32(read_packet->data + 5))) || + (stream_id == (int)ssh2_ntohu32(read_packet->data + 5))) || (!stream_id && (read_packet->data[0] == SSH_MSG_CHANNEL_DATA) && (channel->local.id == read_local_id)) || @@ -2258,8 +2211,8 @@ size_t _libssh2_channel_packet_data_len(LIBSSH2_CHANNEL *channel, * Returns: number of bytes sent, or if it returns a negative number, that is * the error code! */ -ssize_t _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id, - const unsigned char *buf, size_t buflen) +ssize_t ssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id, + const unsigned char *buf, size_t buflen) { int rc = 0; LIBSSH2_SESSION *session = channel->session; @@ -2276,33 +2229,31 @@ ssize_t _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id, if(buflen > 32700) buflen = 32700; - if(channel->write_state == libssh2_NB_state_idle) { + if(channel->write_state == ssh2_NB_state_idle) { unsigned char *s = channel->write_packet; - _libssh2_debug((channel->session, LIBSSH2_TRACE_CONN, - "Writing %ld bytes on channel %u/%u, stream #%d", - (long)buflen, channel->local.id, channel->remote.id, - stream_id)); + ssh2_deb((channel->session, LIBSSH2_TRACE_CONN, + "Writing %ld bytes on channel %u/%u, stream #%d", + (long)buflen, channel->local.id, channel->remote.id, + stream_id)); if(channel->local.close) - return _libssh2_error(channel->session, - LIBSSH2_ERROR_CHANNEL_CLOSED, - "We have already closed this channel"); + return ssh2_err(channel->session, LIBSSH2_ERROR_CHANNEL_CLOSED, + "We have already closed this channel"); else if(channel->local.eof) - return _libssh2_error(channel->session, - LIBSSH2_ERROR_CHANNEL_EOF_SENT, - "EOF has already been received, " - "data might be ignored"); + return ssh2_err(channel->session, LIBSSH2_ERROR_CHANNEL_EOF_SENT, + "EOF has already been received, " + "data might be ignored"); /* drain the incoming flow first, mostly to make sure we get all * pending window adjust packets */ do - rc = _libssh2_transport_read(session); + rc = ssh2_transport_read(session); while(rc > 0); if((rc < 0) && (rc != LIBSSH2_ERROR_EAGAIN)) { - return _libssh2_error(channel->session, rc, - "Failure while draining incoming flow"); + return ssh2_err(channel->session, rc, + "Failure while draining incoming flow"); } if(channel->local.window_size <= 0) { @@ -2321,58 +2272,58 @@ ssize_t _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id, *(s++) = stream_id ? SSH_MSG_CHANNEL_EXTENDED_DATA : SSH_MSG_CHANNEL_DATA; - _libssh2_store_u32(&s, channel->remote.id); + ssh2_store_u32(&s, channel->remote.id); if(stream_id) - _libssh2_store_u32(&s, stream_id); + ssh2_store_u32(&s, stream_id); /* Do not exceed the remote end's limits */ /* REMEMBER local means local as the SOURCE of the data */ if(channel->write_bufwrite > channel->local.window_size) { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Splitting write block due to %u byte " - "window_size on %u/%u/%d", - channel->local.window_size, channel->local.id, - channel->remote.id, stream_id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Splitting write block due to %u byte " + "window_size on %u/%u/%d", + channel->local.window_size, channel->local.id, + channel->remote.id, stream_id)); channel->write_bufwrite = channel->local.window_size; } if(channel->write_bufwrite > channel->local.packet_size) { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Splitting write block due to %u byte " - "packet_size on %u/%u/%d", - channel->local.packet_size, channel->local.id, - channel->remote.id, stream_id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Splitting write block due to %u byte " + "packet_size on %u/%u/%d", + channel->local.packet_size, channel->local.id, + channel->remote.id, stream_id)); channel->write_bufwrite = channel->local.packet_size; } /* store the size here only, the buffer is passed in as-is to - _libssh2_transport_send() */ - _libssh2_store_u32(&s, (uint32_t)channel->write_bufwrite); + ssh2_transport_send() */ + ssh2_store_u32(&s, (uint32_t)channel->write_bufwrite); channel->write_packet_len = s - channel->write_packet; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Sending %ld bytes on channel %u/%u, stream_id=%d", - (long)channel->write_bufwrite, channel->local.id, - channel->remote.id, stream_id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Sending %ld bytes on channel %u/%u, stream_id=%d", + (long)channel->write_bufwrite, channel->local.id, + channel->remote.id, stream_id)); - channel->write_state = libssh2_NB_state_created; + channel->write_state = ssh2_NB_state_created; } - if(channel->write_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, channel->write_packet, - channel->write_packet_len, - buf, channel->write_bufwrite); + if(channel->write_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, channel->write_packet, + channel->write_packet_len, + buf, channel->write_bufwrite); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, rc, "Unable to send channel data"); + return ssh2_err(session, rc, "Unable to send channel data"); } else if(rc) { - channel->write_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Unable to send channel data"); + channel->write_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send channel data"); } /* Shrink local window size */ channel->local.window_size -= (uint32_t)channel->write_bufwrite; wrote += channel->write_bufwrite; - /* Since _libssh2_transport_write() succeeded, we must return + /* Since ssh2_transport_write() succeeded, we must return now to allow the caller to provide the next chunk of data. We cannot move on to send the next piece of data that may @@ -2381,7 +2332,7 @@ ssize_t _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id, both about sent data as well as EAGAIN. By returning short now, the caller calls this function again with new data to send */ - channel->write_state = libssh2_NB_state_idle; + channel->write_state = ssh2_NB_state_idle; return wrote; } @@ -2402,8 +2353,8 @@ ssize_t libssh2_channel_write_ex(LIBSSH2_CHANNEL *channel, int stream_id, return LIBSSH2_ERROR_BAD_USE; BLOCK_ADJUST(rc, channel->session, - _libssh2_channel_write(channel, stream_id, - (const unsigned char *)buf, buflen)); + ssh2_channel_write(channel, stream_id, + (const unsigned char *)buf, buflen)); return rc; } @@ -2416,19 +2367,18 @@ static int channel_send_eof(LIBSSH2_CHANNEL *channel) unsigned char packet[5]; /* packet_type(1) + channelno(4) */ int rc; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Sending EOF on channel %u/%u", - channel->local.id, channel->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, "Sending EOF on channel %u/%u", + channel->local.id, channel->remote.id)); packet[0] = SSH_MSG_CHANNEL_EOF; - _libssh2_htonu32(packet + 1, channel->remote.id); - rc = _libssh2_transport_send(session, packet, 5, NULL, 0); + ssh2_htonu32(packet + 1, channel->remote.id); + rc = ssh2_transport_send(session, packet, 5, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, "Would block sending EOF"); + ssh2_err(session, rc, "Would block sending EOF"); return rc; } else if(rc) { - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send EOF on channel"); + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send EOF on channel"); } channel->local.eof = 1; @@ -2464,23 +2414,23 @@ int libssh2_channel_eof(LIBSSH2_CHANNEL *channel) return LIBSSH2_ERROR_BAD_USE; session = channel->session; - packet = _libssh2_list_first(&session->packets); + packet = ssh2_list_first(&session->packets); while(packet) { - next_packet = _libssh2_list_next(&packet->node); + next_packet = ssh2_list_next(&packet->node); if(packet->data_len < 1) { packet = next_packet; - _libssh2_debug((channel->session, LIBSSH2_TRACE_ERROR, - "Unexpected packet length")); + ssh2_deb((channel->session, LIBSSH2_TRACE_ERROR, + "Unexpected packet length")); continue; } if(((packet->data[0] == SSH_MSG_CHANNEL_DATA) || (packet->data[0] == SSH_MSG_CHANNEL_EXTENDED_DATA)) && ((packet->data_len >= 5) && - (channel->local.id == _libssh2_ntohu32(packet->data + 1)))) { + (channel->local.id == ssh2_ntohu32(packet->data + 1)))) { /* There is data waiting to be read yet, mask the EOF status */ return 0; } @@ -2498,12 +2448,12 @@ static int channel_wait_eof(LIBSSH2_CHANNEL *channel) LIBSSH2_SESSION *session = channel->session; int rc; - if(channel->wait_eof_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Awaiting EOF for channel %u/%u", channel->local.id, - channel->remote.id)); + if(channel->wait_eof_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Awaiting EOF for channel %u/%u", + channel->local.id, channel->remote.id)); - channel->wait_eof_state = libssh2_NB_state_created; + channel->wait_eof_state = ssh2_NB_state_created; } /* @@ -2517,22 +2467,20 @@ static int channel_wait_eof(LIBSSH2_CHANNEL *channel) if((channel->remote.window_size == channel->read_avail) && session->api_block_mode) - return _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_WINDOW_FULL, - "Receiving channel window " - "has been exhausted"); + return ssh2_err(session, LIBSSH2_ERROR_CHANNEL_WINDOW_FULL, + "Receiving channel window has been exhausted"); - rc = _libssh2_transport_read(session); + rc = ssh2_transport_read(session); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc < 0) { - channel->wait_eof_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "_libssh2_transport_read() bailed out"); + channel->wait_eof_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "ssh2_transport_read() bailed out"); } } while(1); - channel->wait_eof_state = libssh2_NB_state_idle; + channel->wait_eof_state = ssh2_NB_state_idle; return 0; } @@ -2552,7 +2500,7 @@ int libssh2_channel_wait_eof(LIBSSH2_CHANNEL *channel) return rc; } -int _libssh2_channel_close(LIBSSH2_CHANNEL *channel) +int ssh2_channel_close(LIBSSH2_CHANNEL *channel) { LIBSSH2_SESSION *session = channel->session; int rc = 0; @@ -2560,7 +2508,7 @@ int _libssh2_channel_close(LIBSSH2_CHANNEL *channel) if(channel->local.close) { /* Already closed, act like we sent another close, * even though we did not... shhhhhh */ - channel->close_state = libssh2_NB_state_idle; + channel->close_state = ssh2_NB_state_idle; return 0; } @@ -2570,48 +2518,46 @@ int _libssh2_channel_close(LIBSSH2_CHANNEL *channel) if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } - _libssh2_error(session, rc, - "Unable to send EOF, but closing channel anyway"); + ssh2_err(session, rc, + "Unable to send EOF, but closing channel anyway"); } } /* ignore if we have received a remote eof or not, as it is now too late for us to wait for it. Continue closing! */ - if(channel->close_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, "Closing channel %u/%u", - channel->local.id, channel->remote.id)); + if(channel->close_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, "Closing channel %u/%u", + channel->local.id, channel->remote.id)); channel->close_packet[0] = SSH_MSG_CHANNEL_CLOSE; - _libssh2_htonu32(channel->close_packet + 1, channel->remote.id); + ssh2_htonu32(channel->close_packet + 1, channel->remote.id); - channel->close_state = libssh2_NB_state_created; + channel->close_state = ssh2_NB_state_created; } - if(channel->close_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, channel->close_packet, 5, - NULL, 0); + if(channel->close_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, channel->close_packet, 5, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, "Would block sending close-channel"); + ssh2_err(session, rc, "Would block sending close-channel"); return rc; } else if(rc) { - _libssh2_error(session, rc, - "Unable to send close-channel request, " - "but closing anyway"); + ssh2_err(session, rc, "Unable to send close-channel request, " + "but closing anyway"); /* skip waiting for the response and fall through to - LIBSSH2_CHANNEL_CLOSE below */ + SSH2_CHANNEL_CLOSE below */ } else - channel->close_state = libssh2_NB_state_sent; + channel->close_state = ssh2_NB_state_sent; } - if(channel->close_state == libssh2_NB_state_sent) { + if(channel->close_state == ssh2_NB_state_sent) { /* We must wait for the remote SSH_MSG_CHANNEL_CLOSE message */ while(!channel->remote.close && !rc && - (session->socket_state != LIBSSH2_SOCKET_DISCONNECTED)) - rc = _libssh2_transport_read(session); + (session->socket_state != SSH2_SOCKET_DISCONNECTED)) + rc = ssh2_transport_read(session); } if(rc != LIBSSH2_ERROR_EAGAIN) { @@ -2622,10 +2568,10 @@ int _libssh2_channel_close(LIBSSH2_CHANNEL *channel) /* We call the callback last in this function to make it keep the local data as long as EAGAIN is returned. */ if(channel->close_cb) { - LIBSSH2_CHANNEL_CLOSE(session, channel); + SSH2_CHANNEL_CLOSE(session, channel); } - channel->close_state = libssh2_NB_state_idle; + channel->close_state = ssh2_NB_state_idle; } /* return 0 or an error */ @@ -2643,7 +2589,7 @@ int libssh2_channel_close(LIBSSH2_CHANNEL *channel) if(!channel) return LIBSSH2_ERROR_BAD_USE; - BLOCK_ADJUST(rc, channel->session, _libssh2_channel_close(channel)); + BLOCK_ADJUST(rc, channel->session, ssh2_channel_close(channel)); return rc; } @@ -2656,17 +2602,17 @@ static int channel_wait_closed(LIBSSH2_CHANNEL *channel) int rc; if(!channel->remote.eof) { - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "libssh2_channel_wait_closed() invoked when " - "channel is not in EOF state"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "libssh2_channel_wait_closed() invoked when " + "channel is not in EOF state"); } - if(channel->wait_closed_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Awaiting close of channel %u/%u", channel->local.id, - channel->remote.id)); + if(channel->wait_closed_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Awaiting close of channel %u/%u", + channel->local.id, channel->remote.id)); - channel->wait_closed_state = libssh2_NB_state_created; + channel->wait_closed_state = ssh2_NB_state_created; } /* @@ -2675,7 +2621,7 @@ static int channel_wait_closed(LIBSSH2_CHANNEL *channel) */ if(!channel->remote.close) { do { - rc = _libssh2_transport_read(session); + rc = ssh2_transport_read(session); if(channel->remote.close) /* it is now closed, move on! */ break; @@ -2684,7 +2630,7 @@ static int channel_wait_closed(LIBSSH2_CHANNEL *channel) return rc; } - channel->wait_closed_state = libssh2_NB_state_idle; + channel->wait_closed_state = ssh2_NB_state_idle; return 0; } @@ -2710,7 +2656,7 @@ int libssh2_channel_wait_closed(LIBSSH2_CHANNEL *channel) * * Returns 0 on success, negative on failure */ -int _libssh2_channel_free(LIBSSH2_CHANNEL *channel) +int ssh2_channel_free(LIBSSH2_CHANNEL *channel) { LIBSSH2_SESSION *session = channel->session; unsigned char channel_id[4]; @@ -2720,18 +2666,18 @@ int _libssh2_channel_free(LIBSSH2_CHANNEL *channel) assert(session); - if(channel->free_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Freeing channel %u/%u resources", channel->local.id, - channel->remote.id)); + if(channel->free_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Freeing channel %u/%u resources", + channel->local.id, channel->remote.id)); - channel->free_state = libssh2_NB_state_created; + channel->free_state = ssh2_NB_state_created; } /* Allow channel freeing even when the socket has lost its connection */ if(!channel->local.close && - (session->socket_state == LIBSSH2_SOCKET_CONNECTED)) { - rc = _libssh2_channel_close(channel); + (session->socket_state == SSH2_SOCKET_CONNECTED)) { + rc = ssh2_channel_close(channel); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; @@ -2740,10 +2686,10 @@ int _libssh2_channel_free(LIBSSH2_CHANNEL *channel) free from happening */ } - channel->free_state = libssh2_NB_state_idle; + channel->free_state = ssh2_NB_state_idle; if(channel->exit_signal) { - LIBSSH2_FREE(session, channel->exit_signal); + SSH2_FREE(session, channel->exit_signal); } /* @@ -2753,78 +2699,78 @@ int _libssh2_channel_free(LIBSSH2_CHANNEL *channel) */ /* Clear out packets meant for this channel */ - _libssh2_htonu32(channel_id, channel->local.id); - while((_libssh2_packet_ask(session, SSH_MSG_CHANNEL_DATA, &data, - &data_len, 1, channel_id, 4) >= 0) || - (_libssh2_packet_ask(session, SSH_MSG_CHANNEL_EXTENDED_DATA, &data, - &data_len, 1, channel_id, 4) >= 0)) { - LIBSSH2_FREE(session, data); + ssh2_htonu32(channel_id, channel->local.id); + while((ssh2_packet_ask(session, SSH_MSG_CHANNEL_DATA, &data, + &data_len, 1, channel_id, 4) >= 0) || + (ssh2_packet_ask(session, SSH_MSG_CHANNEL_EXTENDED_DATA, &data, + &data_len, 1, channel_id, 4) >= 0)) { + SSH2_FREE(session, data); } /* free "channel_type" */ if(channel->channel_type) { - LIBSSH2_FREE(session, channel->channel_type); + SSH2_FREE(session, channel->channel_type); } /* Unlink from channel list */ - _libssh2_list_remove(&channel->node); + ssh2_list_remove(&channel->node); /* * Make sure all memory used in the state variables are free */ if(channel->setenv_packet) { - LIBSSH2_FREE(session, channel->setenv_packet); + SSH2_FREE(session, channel->setenv_packet); } if(channel->reqX11_packet) { - LIBSSH2_FREE(session, channel->reqX11_packet); + SSH2_FREE(session, channel->reqX11_packet); } if(channel->process_packet) { - LIBSSH2_FREE(session, channel->process_packet); + SSH2_FREE(session, channel->process_packet); } /* Free some entries in session-structure, if channel found */ if(session->open_channel == channel) { session->open_channel = NULL; - session->open_state = libssh2_NB_state_idle; + session->open_state = ssh2_NB_state_idle; if(session->open_packet) { - LIBSSH2_FREE(session, session->open_packet); + SSH2_FREE(session, session->open_packet); session->open_packet = NULL; } if(session->open_data) { - LIBSSH2_FREE(session, session->open_data); + SSH2_FREE(session, session->open_data); session->open_data = NULL; } } if(session->pkeyInit_channel == channel) { session->pkeyInit_channel = NULL; - session->pkeyInit_state = libssh2_NB_state_idle; + session->pkeyInit_state = ssh2_NB_state_idle; if(session->pkeyInit_data) { - LIBSSH2_FREE(session, session->pkeyInit_data); + SSH2_FREE(session, session->pkeyInit_data); session->pkeyInit_data = NULL; } } if(session->packAdd_channelp == channel) { session->packAdd_channelp = NULL; - session->packAdd_state = libssh2_NB_state_idle; + session->packAdd_state = ssh2_NB_state_idle; } if(session->scpSend_channel == channel) { session->scpSend_channel = NULL; - session->scpSend_state = libssh2_NB_state_idle; + session->scpSend_state = ssh2_NB_state_idle; if(session->scpSend_command) { - LIBSSH2_FREE(session, session->scpSend_command); + SSH2_FREE(session, session->scpSend_command); session->scpSend_command = NULL; } } if(session->scpRecv_channel == channel) { session->scpRecv_channel = NULL; - session->scpRecv_state = libssh2_NB_state_idle; + session->scpRecv_state = ssh2_NB_state_idle; if(session->scpRecv_command) { - LIBSSH2_FREE(session, session->scpRecv_command); + SSH2_FREE(session, session->scpRecv_command); session->scpRecv_command = NULL; } } - LIBSSH2_FREE(session, channel); + SSH2_FREE(session, channel); return 0; } @@ -2843,7 +2789,7 @@ int libssh2_channel_free(LIBSSH2_CHANNEL *channel) if(!channel) return LIBSSH2_ERROR_BAD_USE; - BLOCK_ADJUST(rc, channel->session, _libssh2_channel_free(channel)); + BLOCK_ADJUST(rc, channel->session, ssh2_channel_free(channel)); return rc; } @@ -2870,17 +2816,16 @@ unsigned long libssh2_channel_window_read_ex( if(read_avail) { size_t bytes_queued = 0; struct packet *next_packet; - struct packet *packet = - _libssh2_list_first(&channel->session->packets); + struct packet *packet = ssh2_list_first(&channel->session->packets); while(packet) { unsigned char packet_type; - next_packet = _libssh2_list_next(&packet->node); + next_packet = ssh2_list_next(&packet->node); if(packet->data_len < 1) { packet = next_packet; - _libssh2_debug((channel->session, LIBSSH2_TRACE_ERROR, - "Unexpected packet length")); + ssh2_deb((channel->session, LIBSSH2_TRACE_ERROR, + "Unexpected packet length")); continue; } @@ -2889,7 +2834,7 @@ unsigned long libssh2_channel_window_read_ex( if(((packet_type == SSH_MSG_CHANNEL_DATA) || (packet_type == SSH_MSG_CHANNEL_EXTENDED_DATA)) && ((packet->data_len >= 5) && - (_libssh2_ntohu32(packet->data + 1) == channel->local.id))) { + (ssh2_ntohu32(packet->data + 1) == channel->local.id))) { bytes_queued += packet->data_len - packet->data_head; } @@ -2946,7 +2891,7 @@ static int channel_signal(LIBSSH2_CHANNEL *channel, LIBSSH2_SESSION *session = channel->session; int retcode = LIBSSH2_ERROR_PROTO; - if(channel->sendsignal_state == libssh2_NB_state_idle) { + if(channel->sendsignal_state == ssh2_NB_state_idle) { unsigned char *s; /* 20 = packet_type(1) + channel(4) + @@ -2955,40 +2900,39 @@ static int channel_signal(LIBSSH2_CHANNEL *channel, channel->sendsignal_packet_len = 20 + signame_len; s = channel->sendsignal_packet = - LIBSSH2_ALLOC(session, channel->sendsignal_packet_len); + SSH2_ALLOC(session, channel->sendsignal_packet_len); if(!channel->sendsignal_packet) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "signal request"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for signal request"); *(s++) = SSH_MSG_CHANNEL_REQUEST; - _libssh2_store_u32(&s, channel->remote.id); - _libssh2_store_str(&s, "signal", sizeof("signal") - 1); + ssh2_store_u32(&s, channel->remote.id); + ssh2_store_str(&s, "signal", sizeof("signal") - 1); *(s++) = 0x00; /* Do not reply */ - _libssh2_store_str(&s, signame, signame_len); + ssh2_store_str(&s, signame, signame_len); - channel->sendsignal_state = libssh2_NB_state_created; + channel->sendsignal_state = ssh2_NB_state_created; } - if(channel->sendsignal_state == libssh2_NB_state_created) { + if(channel->sendsignal_state == ssh2_NB_state_created) { int rc; - rc = _libssh2_transport_send(session, channel->sendsignal_packet, - channel->sendsignal_packet_len, NULL, 0); + rc = ssh2_transport_send(session, channel->sendsignal_packet, + channel->sendsignal_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, rc, "Would block sending signal request"); + ssh2_err(session, rc, "Would block sending signal request"); return rc; } else if(rc) { - LIBSSH2_FREE(session, channel->sendsignal_packet); - channel->sendsignal_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Unable to send signal packet"); + SSH2_FREE(session, channel->sendsignal_packet); + channel->sendsignal_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send signal packet"); } - LIBSSH2_FREE(session, channel->sendsignal_packet); + SSH2_FREE(session, channel->sendsignal_packet); retcode = LIBSSH2_ERROR_NONE; } - channel->sendsignal_state = libssh2_NB_state_idle; + channel->sendsignal_state = ssh2_NB_state_idle; return retcode; } diff --git a/src/channel.h b/src/channel.h index 420c80e7..e1d8937f 100644 --- a/src/channel.h +++ b/src/channel.h @@ -47,16 +47,16 @@ * * Always non-blocking. */ -int _libssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel, - uint32_t adjustment, - unsigned char force, - unsigned int *store); +int ssh2_channel_receive_window_adjust(LIBSSH2_CHANNEL *channel, + uint32_t adjustment, + unsigned char force, + unsigned int *store); /* * Flush data from one (or all) stream * Returns number of bytes flushed, or negative on failure */ -int _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid); +int ssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid); /* * Make sure a channel is closed, then remove the channel from the session @@ -64,33 +64,33 @@ int _libssh2_channel_flush(LIBSSH2_CHANNEL *channel, int streamid); * * Returns 0 on success, negative on failure */ -int _libssh2_channel_free(LIBSSH2_CHANNEL *channel); +int ssh2_channel_free(LIBSSH2_CHANNEL *channel); -int _libssh2_channel_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode); +int ssh2_channel_extended_data(LIBSSH2_CHANNEL *channel, int ignore_mode); /* * Send data to a channel */ -ssize_t _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id, - const unsigned char *buf, size_t buflen); +ssize_t ssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id, + const unsigned char *buf, size_t buflen); /* * Establish a generic session channel */ -LIBSSH2_CHANNEL *_libssh2_channel_open(LIBSSH2_SESSION *session, - const char *channel_type, - uint32_t channel_type_len, - uint32_t window_size, - uint32_t packet_size, - const unsigned char *message, - size_t message_len); +LIBSSH2_CHANNEL *ssh2_channel_open(LIBSSH2_SESSION *session, + const char *channel_type, + uint32_t channel_type_len, + uint32_t window_size, + uint32_t packet_size, + const unsigned char *message, + size_t message_len); /* * Primitive for libssh2_channel_(shell|exec|subsystem) */ -int _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, - const char *request, size_t request_len, - const char *message, size_t message_len); +int ssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, + const char *request, size_t request_len, + const char *message, size_t message_len); /* * Read data from a channel @@ -99,18 +99,18 @@ int _libssh2_channel_process_startup(LIBSSH2_CHANNEL *channel, * complete. If we read stuff from the wire but it was no payload data to fill * in the buffer with, we MUST make sure to return PACKET_EAGAIN. */ -ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, - char *buf, size_t buflen); +ssize_t ssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, + char *buf, size_t buflen); -uint32_t _libssh2_channel_nextid(LIBSSH2_SESSION *session); +uint32_t ssh2_channel_nextid(LIBSSH2_SESSION *session); -LIBSSH2_CHANNEL *_libssh2_channel_locate(LIBSSH2_SESSION *session, - uint32_t channel_id); +LIBSSH2_CHANNEL *ssh2_channel_locate(LIBSSH2_SESSION *session, + uint32_t channel_id); -size_t _libssh2_channel_packet_data_len(LIBSSH2_CHANNEL *channel, - int stream_id); +size_t ssh2_channel_packet_data_len(LIBSSH2_CHANNEL *channel, + int stream_id); -int _libssh2_channel_close(LIBSSH2_CHANNEL *channel); +int ssh2_channel_close(LIBSSH2_CHANNEL *channel); /* * Stop listening on a remote port and free the listener @@ -118,6 +118,6 @@ int _libssh2_channel_close(LIBSSH2_CHANNEL *channel); * * Return 0 on success, LIBSSH2_ERROR_EAGAIN if would block, -1 on error */ -int _libssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener); +int ssh2_channel_forward_cancel(LIBSSH2_LISTENER *listener); #endif /* LIBSSH2_CHANNEL_H */ diff --git a/src/cipher-chachapoly.c b/src/cipher-chachapoly.c index c8ef0cc5..79c9c8b4 100644 --- a/src/cipher-chachapoly.c +++ b/src/cipher-chachapoly.c @@ -63,7 +63,7 @@ int chachapoly_crypt(struct chachapoly_ctx *ctx, libssh2_uint64_t seqnr, */ memset(poly_key, 0, sizeof(poly_key)); ptr = &seqbuf[0]; - _libssh2_store_u64(&ptr, seqnr); + ssh2_store_u64(&ptr, seqnr); chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL); chacha_encrypt_bytes(&ctx->main_ctx, poly_key, poly_key, sizeof(poly_key)); @@ -112,10 +112,10 @@ int chachapoly_get_length(struct chachapoly_ctx *ctx, unsigned int *plenp, if(len < 4) return -1; ptr = &seqbuf[0]; - _libssh2_store_u64(&ptr, seqnr); + ssh2_store_u64(&ptr, seqnr); chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL); chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4); - *plenp = _libssh2_ntohu32(buf); + *plenp = ssh2_ntohu32(buf); return 0; } diff --git a/src/comp.c b/src/comp.c index 47fd5a4a..729a12bc 100644 --- a/src/comp.c +++ b/src/comp.c @@ -120,14 +120,14 @@ static voidpf comp_method_zlib_alloc(voidpf opaque, uInt items, uInt size) if(items && (SIZE_MAX / (size_t)items) < (size_t)size) return NULL; - return (voidpf)LIBSSH2_ALLOC(session, items * (size_t)size); + return (voidpf)SSH2_ALLOC(session, items * (size_t)size); } static void comp_method_zlib_free(voidpf opaque, voidpf address) { LIBSSH2_SESSION *session = (LIBSSH2_SESSION *)opaque; - LIBSSH2_FREE(session, address); + SSH2_FREE(session, address); } /* @@ -139,11 +139,11 @@ static int comp_method_zlib_init(LIBSSH2_SESSION *session, int compr, z_stream *strm; int status; - strm = LIBSSH2_CALLOC(session, sizeof(z_stream)); + strm = SSH2_CALLOC(session, sizeof(z_stream)); if(!strm) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "zlib compression/decompression"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "zlib compression/decompression"); } strm->opaque = (voidpf)session; @@ -159,9 +159,9 @@ static int comp_method_zlib_init(LIBSSH2_SESSION *session, int compr, } if(status != Z_OK) { - LIBSSH2_FREE(session, strm); - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "unhandled zlib error %d", status)); + SSH2_FREE(session, strm); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "unhandled zlib error %d", + status)); return LIBSSH2_ERROR_COMPRESS; } *abstract = strm; @@ -198,10 +198,10 @@ static int comp_method_zlib_comp(LIBSSH2_SESSION *session, return 0; } - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "unhandled zlib compression error %d, avail_out %u", - status, strm->avail_out)); - return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, "compression failure"); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "unhandled zlib compression error %d, avail_out %u", + status, strm->avail_out)); + return ssh2_err(session, LIBSSH2_ERROR_ZLIB, "compression failure"); } /* @@ -227,8 +227,8 @@ static int comp_method_zlib_decomp(LIBSSH2_SESSION *session, /* If strm is null, then we have not yet been initialized. */ if(!strm) - return _libssh2_error(session, LIBSSH2_ERROR_COMPRESS, - "decompression uninitialized"); + return ssh2_err(session, LIBSSH2_ERROR_COMPRESS, + "decompression uninitialized"); /* In practice they never come smaller than this */ if(out_maxlen < 25) @@ -239,12 +239,12 @@ static int comp_method_zlib_decomp(LIBSSH2_SESSION *session, strm->next_in = (z_const Bytef *)src; strm->avail_in = (uInt)src_len; - strm->next_out = (Bytef *)LIBSSH2_ALLOC(session, (uInt)out_maxlen); + strm->next_out = (Bytef *)SSH2_ALLOC(session, (uInt)out_maxlen); out = (char *)strm->next_out; strm->avail_out = (uInt)out_maxlen; if(!strm->next_out) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate decompression buffer"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate decompression buffer"); /* Loop until it is all inflated or hit error */ for(;;) { @@ -265,38 +265,38 @@ static int comp_method_zlib_decomp(LIBSSH2_SESSION *session, if((out_maxlen - strm->avail_out) == 0) { /* nothing was decompressed */ - LIBSSH2_FREE(session, out); - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "zlib empty src error %d", status)); - return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, - "decompression failure"); + SSH2_FREE(session, out); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "zlib empty src error %d", status)); + return ssh2_err(session, LIBSSH2_ERROR_ZLIB, + "decompression failure"); } break; } else { /* error state */ - LIBSSH2_FREE(session, out); - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "unhandled zlib error %d", status)); - return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, - "decompression failure"); + SSH2_FREE(session, out); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "unhandled zlib error %d", + status)); + return ssh2_err(session, LIBSSH2_ERROR_ZLIB, + "decompression failure"); } if(out_maxlen > payload_limit || out_maxlen > SIZE_MAX / 2) { - LIBSSH2_FREE(session, out); - return _libssh2_error(session, LIBSSH2_ERROR_ZLIB, - "Excessive growth in decompression phase"); + SSH2_FREE(session, out); + return ssh2_err(session, LIBSSH2_ERROR_ZLIB, + "Excessive growth in decompression phase"); } /* If we get here we need to grow the output buffer and try again */ out_ofs = out_maxlen - strm->avail_out; out_maxlen *= 2; - newout = LIBSSH2_REALLOC(session, out, out_maxlen); + newout = SSH2_REALLOC(session, out, out_maxlen); if(!newout) { - LIBSSH2_FREE(session, out); - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to expand decompression buffer"); + SSH2_FREE(session, out); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to expand decompression buffer"); } out = newout; strm->next_out = (Bytef *)out + out_ofs; @@ -322,7 +322,7 @@ static int comp_method_zlib_dtor(LIBSSH2_SESSION *session, int compr, deflateEnd(strm); else inflateEnd(strm); - LIBSSH2_FREE(session, strm); + SSH2_FREE(session, strm); } *abstract = NULL; @@ -367,7 +367,7 @@ static const struct comp_method *no_comp_methods[] = { NULL }; -const struct comp_method **_libssh2_comp_methods(LIBSSH2_SESSION *session) +const struct comp_method **ssh2_comp_methods(LIBSSH2_SESSION *session) { if(session->flag.compress) return comp_methods; diff --git a/src/comp.h b/src/comp.h index be4209cd..3774bbd4 100644 --- a/src/comp.h +++ b/src/comp.h @@ -40,6 +40,6 @@ #include "libssh2_priv.h" -const struct comp_method **_libssh2_comp_methods(LIBSSH2_SESSION *session); +const struct comp_method **ssh2_comp_methods(LIBSSH2_SESSION *session); #endif /* LIBSSH2_COMP_H */ diff --git a/src/crypt.c b/src/crypt.c index d38f71b2..1e4d16b7 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -83,8 +83,8 @@ static const struct crypt_method crypt_method_none = { struct crypt_ctx { int encrypt; - LIBSSH2_CIPHER_T(algo); - libssh2_cipher_ctx h; + SSH2_CIPHER_T(algo); + ssh2_cipher_ctx h; struct chachapoly_ctx chachapoly_ctx; }; @@ -94,14 +94,14 @@ static int crypt_init(LIBSSH2_SESSION *session, unsigned char *secret, int *free_secret, int encrypt, void **abstract) { - struct crypt_ctx *ctx = LIBSSH2_ALLOC(session, sizeof(struct crypt_ctx)); + struct crypt_ctx *ctx = SSH2_ALLOC(session, sizeof(struct crypt_ctx)); if(!ctx) return LIBSSH2_ERROR_ALLOC; ctx->encrypt = encrypt; ctx->algo = method->algo; - if(_libssh2_cipher_init(&ctx->h, ctx->algo, iv, secret, encrypt)) { - LIBSSH2_FREE(session, ctx); + if(ssh2_cipher_init(&ctx->h, ctx->algo, iv, secret, encrypt)) { + SSH2_FREE(session, ctx); return -1; } *abstract = ctx; @@ -120,16 +120,16 @@ static int crypt_encrypt(LIBSSH2_SESSION *session, struct crypt_ctx *cctx = *(struct crypt_ctx **)abstract; (void)session; (void)seqno; - return _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, buf, - buf_len, firstlast); + return ssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, buf, + buf_len, firstlast); } static int crypt_dtor(LIBSSH2_SESSION *session, void **abstract) { struct crypt_ctx **cctx = (struct crypt_ctx **)abstract; if(cctx && *cctx) { - _libssh2_cipher_dtor(&(*cctx)->h); - LIBSSH2_FREE(session, *cctx); + ssh2_cipher_dtor(&(*cctx)->h); + SSH2_FREE(session, *cctx); *abstract = NULL; } return 0; @@ -143,12 +143,12 @@ static const struct crypt_method crypt_method_aes256_gcm = { 12, /* initial value length */ 32, /* secret length -- 32*8 == 256bit */ 16, /* length of the authentication tag */ - LIBSSH2_CRYPT_FLAG_INTEGRATED_MAC | LIBSSH2_CRYPT_FLAG_PKTLEN_AAD, + SSH2_CRYPT_FLAG_INTEGRATED_MAC | SSH2_CRYPT_FLAG_PKTLEN_AAD, &crypt_init, NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes256gcm + ssh2_cipher_aes256gcm }; static const struct crypt_method crypt_method_aes128_gcm = { @@ -158,12 +158,12 @@ static const struct crypt_method crypt_method_aes128_gcm = { 12, /* initial value length */ 16, /* secret length -- 16*8 == 128bit */ 16, /* length of the authentication tag */ - LIBSSH2_CRYPT_FLAG_INTEGRATED_MAC | LIBSSH2_CRYPT_FLAG_PKTLEN_AAD, + SSH2_CRYPT_FLAG_INTEGRATED_MAC | SSH2_CRYPT_FLAG_PKTLEN_AAD, &crypt_init, NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes128gcm + ssh2_cipher_aes128gcm }; #endif @@ -180,7 +180,7 @@ static const struct crypt_method crypt_method_aes128_ctr = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes128ctr + ssh2_cipher_aes128ctr }; static const struct crypt_method crypt_method_aes192_ctr = { @@ -195,7 +195,7 @@ static const struct crypt_method crypt_method_aes192_ctr = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes192ctr + ssh2_cipher_aes192ctr }; static const struct crypt_method crypt_method_aes256_ctr = { @@ -210,7 +210,7 @@ static const struct crypt_method crypt_method_aes256_ctr = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes256ctr + ssh2_cipher_aes256ctr }; #endif @@ -227,7 +227,7 @@ static const struct crypt_method crypt_method_aes128_cbc = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes128 + ssh2_cipher_aes128 }; static const struct crypt_method crypt_method_aes192_cbc = { @@ -242,7 +242,7 @@ static const struct crypt_method crypt_method_aes192_cbc = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes192 + ssh2_cipher_aes192 }; static const struct crypt_method crypt_method_aes256_cbc = { @@ -257,7 +257,7 @@ static const struct crypt_method crypt_method_aes256_cbc = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes256 + ssh2_cipher_aes256 }; /* rijndael-cbc@lysator.liu.se == aes256-cbc */ @@ -273,7 +273,7 @@ static const struct crypt_method crypt_method_rijndael_cbc_lysator_liu_se = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_aes256 + ssh2_cipher_aes256 }; #endif /* LIBSSH2_AES_CBC */ @@ -290,7 +290,7 @@ static const struct crypt_method crypt_method_blowfish_cbc = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_blowfish + ssh2_cipher_blowfish }; #endif /* LIBSSH2_BLOWFISH */ @@ -307,7 +307,7 @@ static const struct crypt_method crypt_method_arcfour = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_arcfour + ssh2_cipher_arcfour }; static int crypt_init_arcfour128(LIBSSH2_SESSION *session, @@ -325,8 +325,8 @@ static int crypt_init_arcfour128(LIBSSH2_SESSION *session, unsigned char block[8]; size_t discard = 1536; for(; discard; discard -= 8) - _libssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block, - method->blocksize, MIDDLE_BLOCK); + ssh2_cipher_crypt(&cctx->h, cctx->algo, cctx->encrypt, block, + method->blocksize, MIDDLE_BLOCK); /* Not all middle, but here it does not matter */ } @@ -345,7 +345,7 @@ static const struct crypt_method crypt_method_arcfour128 = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_arcfour + ssh2_cipher_arcfour }; #endif /* LIBSSH2_RC4 */ @@ -362,7 +362,7 @@ static const struct crypt_method crypt_method_cast128_cbc = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_cast5 + ssh2_cipher_cast5 }; #endif /* LIBSSH2_CAST */ @@ -379,7 +379,7 @@ static const struct crypt_method crypt_method_3des_cbc = { NULL, &crypt_encrypt, &crypt_dtor, - libssh2_cipher_3des + ssh2_cipher_3des }; #endif @@ -389,7 +389,7 @@ static int crypt_init_chacha20_poly(LIBSSH2_SESSION *session, unsigned char *secret, int *free_secret, int encrypt, void **abstract) { - struct crypt_ctx *ctx = LIBSSH2_ALLOC(session, sizeof(struct crypt_ctx)); + struct crypt_ctx *ctx = SSH2_ALLOC(session, sizeof(struct crypt_ctx)); (void)iv; @@ -400,7 +400,7 @@ static int crypt_init_chacha20_poly(LIBSSH2_SESSION *session, ctx->algo = method->algo; if(chachapoly_init(&ctx->chachapoly_ctx, secret, method->secret_len)) { - LIBSSH2_FREE(session, ctx); + SSH2_FREE(session, ctx); return -1; } @@ -469,7 +469,7 @@ static int crypt_dtor_chacha20_poly(LIBSSH2_SESSION *session, void **abstract) { struct crypt_ctx **cctx = (struct crypt_ctx **)abstract; if(cctx && *cctx) { - LIBSSH2_FREE(session, *cctx); + SSH2_FREE(session, *cctx); *abstract = NULL; } return 0; @@ -482,12 +482,12 @@ static const struct crypt_method crypt_method_chacha20_poly1305_openssh = { 0, /* initial value length */ 64, /* secret length */ 16, /* length of the auth_tag */ - LIBSSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET, /* flags */ + SSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET, /* flags */ &crypt_init_chacha20_poly, &crypt_get_length_chacha20_poly, &crypt_encrypt_chacha20_poly_buffer, &crypt_dtor_chacha20_poly, - libssh2_cipher_chacha20 /* not actually used */ + ssh2_cipher_chacha20 /* not actually used */ }; /* These are the crypt methods that are available to be negotiated. Methods @@ -529,7 +529,7 @@ static const struct crypt_method *crypt_methods[] = { }; /* Expose to kex.c */ -const struct crypt_method **libssh2_crypt_methods(void) +const struct crypt_method **ssh2_crypt_methods(void) { return crypt_methods; } diff --git a/src/crypto.h b/src/crypto.h index 712c0b1d..c3d27004 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -56,218 +56,214 @@ #endif /* return: success = 1, error = 0 */ -int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx); +int ssh2_hmac_ctx_init(ssh2_hmac_ctx *ctx); #if LIBSSH2_MD5 -int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen); +int ssh2_hmac_md5_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen); #endif #if LIBSSH2_HMAC_RIPEMD -int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen); +int ssh2_hmac_ripemd160_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen); #endif -int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen); -int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen); -int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen); -int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, - const void *data, size_t datalen); -int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data); -void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx); +int ssh2_hmac_sha1_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen); +int ssh2_hmac_sha256_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen); +int ssh2_hmac_sha512_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen); +int ssh2_hmac_update(ssh2_hmac_ctx *ctx, const void *data, size_t datalen); +int ssh2_hmac_final(ssh2_hmac_ctx *ctx, void *data); +void ssh2_hmac_cleanup(ssh2_hmac_ctx *ctx); -#define LIBSSH2_ED25519_KEY_LEN 32 -#define LIBSSH2_ED25519_PRIVATE_KEY_LEN 64 -#define LIBSSH2_ED25519_SIG_LEN 64 -#define LIBSSH2_EC_P256_PUBLIC_KEY_LEN 65 -#define LIBSSH2_EC_P384_PUBLIC_KEY_LEN 97 -#define LIBSSH2_MLKEM_SHARED_SECRET_LEN 32 -#define LIBSSH2_MLKEM_512_PRIVATE_KEY_LEN 1632 -#define LIBSSH2_MLKEM_512_PUBLIC_KEY_LEN 800 -#define LIBSSH2_MLKEM_512_CIPHERTEXT 768 -#define LIBSSH2_MLKEM_768_PRIVATE_KEY_LEN 2400 -#define LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN 1184 -#define LIBSSH2_MLKEM_768_CIPHERTEXT 1088 -#define LIBSSH2_MLKEM_1024_PRIVATE_KEY_LEN 3168 -#define LIBSSH2_MLKEM_1024_PUBLIC_KEY_LEN 1568 -#define LIBSSH2_MLKEM_1024_CIPHERTEXT 1568 +#define SSH2_ED25519_KEY_LEN 32 +#define SSH2_ED25519_PRIVATE_KEY_LEN 64 +#define SSH2_ED25519_SIG_LEN 64 + +#define SSH2_EC_P256_PUBLIC_KEY_LEN 65 +#define SSH2_EC_P384_PUBLIC_KEY_LEN 97 + +#define SSH2_MLKEM_SHARED_SECRET_LEN 32 +#define SSH2_MLKEM_512_PRIVATE_KEY_LEN 1632 +#define SSH2_MLKEM_512_PUBLIC_KEY_LEN 800 +#define SSH2_MLKEM_512_CIPHERTEXT 768 +#define SSH2_MLKEM_768_PRIVATE_KEY_LEN 2400 +#define SSH2_MLKEM_768_PUBLIC_KEY_LEN 1184 +#define SSH2_MLKEM_768_CIPHERTEXT 1088 +#define SSH2_MLKEM_1024_PRIVATE_KEY_LEN 3168 +#define SSH2_MLKEM_1024_PUBLIC_KEY_LEN 1568 +#define SSH2_MLKEM_1024_CIPHERTEXT 1568 #if LIBSSH2_RSA -int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, - const unsigned char *edata, unsigned long elen, - const unsigned char *ndata, unsigned long nlen, - const unsigned char *ddata, unsigned long dlen, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *e1data, unsigned long e1len, - const unsigned char *e2data, unsigned long e2len, - const unsigned char *coeffdata, unsigned long coefflen); -int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); +int ssh2_rsa_new(ssh2_rsa_ctx **rsa, + const unsigned char *edata, unsigned long elen, + const unsigned char *ndata, unsigned long nlen, + const unsigned char *ddata, unsigned long dlen, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *e1data, unsigned long e1len, + const unsigned char *e2data, unsigned long e2len, + const unsigned char *coeffdata, unsigned long coefflen); +int ssh2_rsa_new_private(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); #if LIBSSH2_RSA_SHA1 -int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len); -int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx *rsactx, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len); +int ssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len); +int ssh2_rsa_sha1_verify(ssh2_rsa_ctx *rsactx, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len); #endif #if LIBSSH2_RSA_SHA2 -int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len); -int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, - size_t hash_len, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len); +int ssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len); +int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsactx, + size_t hash_len, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len); #endif -int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase); +int ssh2_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase); #endif #if LIBSSH2_DSA -int _libssh2_dsa_new(libssh2_dsa_ctx **dsa, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *gdata, unsigned long glen, - const unsigned char *ydata, unsigned long ylen, - const unsigned char *x, unsigned long x_len); -int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); -int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, - const unsigned char *sig, - const unsigned char *m, size_t m_len); -int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx *dsactx, - const unsigned char *hash, - size_t hash_len, unsigned char *sig); -int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase); +int ssh2_dsa_new(ssh2_dsa_ctx **dsa, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *gdata, unsigned long glen, + const unsigned char *ydata, unsigned long ylen, + const unsigned char *x, unsigned long x_len); +int ssh2_dsa_new_private(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); +int ssh2_dsa_sha1_verify(ssh2_dsa_ctx *dsactx, + const unsigned char *sig, + const unsigned char *m, size_t m_len); +int ssh2_dsa_sha1_sign(ssh2_dsa_ctx *dsactx, + const unsigned char *hash, + size_t hash_len, unsigned char *sig); +int ssh2_dsa_new_private_frommemory(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase); #endif #if LIBSSH2_ECDSA -int _libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx **ec_ctx, - const unsigned char *k, - size_t k_len, - libssh2_curve_type curve); +int ssh2_ecdsa_curve_name_with_octal_new(ssh2_ecdsa_ctx **ec_ctx, + const unsigned char *k, + size_t k_len, + ssh2_curve_type curve); -int _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); +int ssh2_ecdsa_new_private(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); -int _libssh2_ecdsa_new_private_sk(libssh2_ecdsa_ctx **ec_ctx, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase); +int ssh2_ecdsa_new_private_sk(ssh2_ecdsa_ctx **ec_ctx, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase); -int _libssh2_ecdsa_verify(libssh2_ecdsa_ctx *ec_ctx, - const unsigned char *r, size_t r_len, - const unsigned char *s, size_t s_len, - const unsigned char *m, size_t m_len); +int ssh2_ecdsa_verify(ssh2_ecdsa_ctx *ec_ctx, + const unsigned char *r, size_t r_len, + const unsigned char *s, size_t s_len, + const unsigned char *m, size_t m_len); -int _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, - libssh2_ec_key **out_private_key, - unsigned char **out_public_key_octal, - size_t *out_public_key_octal_len, - libssh2_curve_type curve_type); +int ssh2_ecdsa_create_key(LIBSSH2_SESSION *session, + ssh2_ec_key **out_private_key, + unsigned char **out_public_key_octal, + size_t *out_public_key_octal_len, + ssh2_curve_type curve_type); -int _libssh2_ecdh_gen_k(libssh2_bn **k, libssh2_ec_key *private_key, - const unsigned char *server_public_key, - size_t server_public_key_len); +int ssh2_ecdh_gen_k(ssh2_bn **k, ssh2_ec_key *private_key, + const unsigned char *server_public_key, + size_t server_public_key_len); -int _libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx, - const unsigned char *hash, size_t hash_len, - unsigned char **signature, size_t *signature_len); +int ssh2_ecdsa_sign(LIBSSH2_SESSION *session, ssh2_ecdsa_ctx *ec_ctx, + const unsigned char *hash, size_t hash_len, + unsigned char **signature, size_t *signature_len); -int _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase); +int ssh2_ecdsa_new_private_frommemory(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase); -int _libssh2_ecdsa_new_private_frommemory_sk(libssh2_ecdsa_ctx **ec_ctx, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase); +int ssh2_ecdsa_new_private_frommemory_sk(ssh2_ecdsa_ctx **ec_ctx, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase); -libssh2_curve_type _libssh2_ecdsa_get_curve_type(libssh2_ecdsa_ctx *ec_ctx); +ssh2_curve_type ssh2_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ec_ctx); -int _libssh2_ecdsa_curve_type_from_name(const char *name, - libssh2_curve_type *out_type); +int ssh2_ecdsa_curve_type_from_name(const char *name, + ssh2_curve_type *out_type); #endif /* LIBSSH2_ECDSA */ #if LIBSSH2_ED25519 -int _libssh2_curve25519_new(LIBSSH2_SESSION *session, uint8_t **out_public_key, - uint8_t **out_private_key); +int ssh2_curve25519_new(LIBSSH2_SESSION *session, uint8_t **out_public_key, + uint8_t **out_private_key); -int _libssh2_curve25519_gen_k( - libssh2_bn **k, - uint8_t private_key[LIBSSH2_ED25519_KEY_LEN], - uint8_t server_public_key[LIBSSH2_ED25519_KEY_LEN]); +int ssh2_curve25519_gen_k( + ssh2_bn **k, + uint8_t private_key[SSH2_ED25519_KEY_LEN], + uint8_t server_public_key[SSH2_ED25519_KEY_LEN]); -int _libssh2_ed25519_verify(libssh2_ed25519_ctx *ctx, const uint8_t *s, - size_t s_len, const uint8_t *m, size_t m_len); +int ssh2_ed25519_verify(ssh2_ed25519_ctx *ctx, const uint8_t *s, + size_t s_len, const uint8_t *m, size_t m_len); -int _libssh2_ed25519_new_private(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const uint8_t *passphrase); +int ssh2_ed25519_new_private(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const uint8_t *passphrase); -int _libssh2_ed25519_new_private_sk(libssh2_ed25519_ctx **ed_ctx, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - LIBSSH2_SESSION *session, - const char *filename, - const uint8_t *passphrase); - -int _libssh2_ed25519_new_public(libssh2_ed25519_ctx **ed_ctx, +int ssh2_ed25519_new_private_sk(ssh2_ed25519_ctx **ed_ctx, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, LIBSSH2_SESSION *session, - const unsigned char *raw_pub_key, - const size_t key_len); + const char *filename, + const uint8_t *passphrase); -int _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, - uint8_t **out_sig, size_t *out_sig_len, - const uint8_t *message, size_t message_len); +int ssh2_ed25519_new_public(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const unsigned char *raw_pub_key, + const size_t key_len); -int _libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase); +int ssh2_ed25519_sign(ssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, + uint8_t **out_sig, size_t *out_sig_len, + const uint8_t *message, size_t message_len); -int _libssh2_ed25519_new_private_frommemory_sk( - libssh2_ed25519_ctx **ed_ctx, +int ssh2_ed25519_new_private_frommemory(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase); + +int ssh2_ed25519_new_private_frommemory_sk( + ssh2_ed25519_ctx **ed_ctx, unsigned char *flags, const char **application, const unsigned char **key_handle, @@ -280,72 +276,68 @@ int _libssh2_ed25519_new_private_frommemory_sk( #endif /* LIBSSH2_ED25519 */ #if LIBSSH2_MLKEM -int _libssh2_mlkem_new(LIBSSH2_SESSION *session, - int ml_kem_size, - unsigned char **out_public_key, - unsigned char **out_private_key); +int ssh2_mlkem_new(LIBSSH2_SESSION *session, + int ml_kem_size, + unsigned char **out_public_key, + unsigned char **out_private_key); -int _libssh2_mlkem_get_sk(unsigned char *out_shared_key, - int ml_kem_size, - uint8_t *private_key, - uint8_t *server_ciphertext); +int ssh2_mlkem_get_sk(unsigned char *out_shared_key, + int ml_kem_size, + uint8_t *private_key, + uint8_t *server_ciphertext); #endif /* LIBSSH2_MLKEM */ -int _libssh2_cipher_init(libssh2_cipher_ctx *h, - LIBSSH2_CIPHER_T(algo), - unsigned char *iv, - unsigned char *secret, int encrypt); +int ssh2_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo), + unsigned char *iv, unsigned char *secret, int encrypt); -int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, - LIBSSH2_CIPHER_T(algo), - int encrypt, unsigned char *block, size_t blocksize, - int firstlast); +int ssh2_cipher_crypt(ssh2_cipher_ctx *ctx, SSH2_CIPHER_T(algo), + int encrypt, unsigned char *block, size_t blocksize, + int firstlast); -int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, +int ssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekey, + const char *passphrase); + +int ssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase); + +int ssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, unsigned char **method, size_t *method_len, unsigned char **pubkeydata, size_t *pubkeydata_len, - const char *privatekey, + int *algorithm, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + const char *privatekeydata, + size_t privatekeydata_len, const char *passphrase); -int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase); - -int _libssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - int *algorithm, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase); - /** * @abstract Returns supported algorithms used for upgrading public * key signing RFC 8332 * @discussion Based on the incoming key_method value, this function * returns supported algorithms that can upgrade the key method - * @related _libssh2_key_sign_algorithm() * @param key_method current key method, usually the default key sig method * @param key_method_len length of the key method buffer * @result comma separated list of supported upgrade options per RFC 8332, if * there is no upgrade option return NULL */ -const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session, - unsigned char *key_method, - size_t key_method_len); +const char *ssh2_supported_key_sign_algs(LIBSSH2_SESSION *session, + unsigned char *key_method, + size_t key_method_len); #endif /* LIBSSH2_CRYPTO_H */ diff --git a/src/global.c b/src/global.c index 3dd4dfc0..0ac90ffd 100644 --- a/src/global.c +++ b/src/global.c @@ -47,7 +47,7 @@ LIBSSH2_API int libssh2_init(int flags) { if(s_initialized == 0 && !(flags & LIBSSH2_INIT_NO_CRYPTO)) { - libssh2_crypto_init(); + ssh2_crypto_init(); } s_initialized++; @@ -66,11 +66,11 @@ void libssh2_exit(void) if(s_initialized == 0 && !(s_init_flags & LIBSSH2_INIT_NO_CRYPTO)) { - libssh2_crypto_exit(); + ssh2_crypto_exit(); } } -void _libssh2_init_if_needed(void) +void ssh2_init_if_needed(void) { if(s_initialized == 0) (void)libssh2_init(0); diff --git a/src/hostkey.c b/src/hostkey.c index 4569cb31..e94a02b9 100644 --- a/src/hostkey.c +++ b/src/hostkey.c @@ -61,7 +61,7 @@ static int hostkey_method_ssh_rsa_init(LIBSSH2_SESSION *session, size_t hostkey_data_len, void **abstract) { - libssh2_rsa_ctx *rsactx; + ssh2_rsa_ctx *rsactx; unsigned char *e, *n, *type; size_t e_len, n_len, type_len; struct string_buf buf; @@ -72,16 +72,15 @@ static int hostkey_method_ssh_rsa_init(LIBSSH2_SESSION *session, } if(hostkey_data_len < 19) { - _libssh2_debug((session, LIBSSH2_TRACE_ERROR, - "host key length too short")); + ssh2_deb((session, LIBSSH2_TRACE_ERROR, "host key length too short")); return -1; } - buf.data = (unsigned char *)LIBSSH2_UNCONST(hostkey_data); + buf.data = (unsigned char *)SSH2_UNCONST(hostkey_data); buf.dataptr = buf.data; buf.len = hostkey_data_len; - if(_libssh2_get_string(&buf, &type, &type_len)) { + if(ssh2_get_string(&buf, &type, &type_len)) { return -1; } @@ -102,25 +101,25 @@ static int hostkey_method_ssh_rsa_init(LIBSSH2_SESSION *session, else #endif { - _libssh2_debug((session, LIBSSH2_TRACE_ERROR, - "unexpected rsa type: %.*s", (int)type_len, type)); + ssh2_deb((session, LIBSSH2_TRACE_ERROR, "unexpected rsa type: %.*s", + (int)type_len, type)); return -1; } - if(_libssh2_get_string(&buf, &e, &e_len)) + if(ssh2_get_string(&buf, &e, &e_len)) return -1; - if(_libssh2_get_string(&buf, &n, &n_len)) + if(ssh2_get_string(&buf, &n, &n_len)) return -1; - if(!_libssh2_eob(&buf)) + if(!ssh2_eob(&buf)) return -1; - if(_libssh2_rsa_new(&rsactx, - e, (unsigned long)e_len, - n, (unsigned long)n_len, - NULL, 0, NULL, 0, NULL, 0, - NULL, 0, NULL, 0, NULL, 0)) { + if(ssh2_rsa_new(&rsactx, + e, (unsigned long)e_len, + n, (unsigned long)n_len, + NULL, 0, NULL, 0, NULL, 0, + NULL, 0, NULL, 0, NULL, 0)) { return -1; } @@ -137,7 +136,7 @@ static int hostkey_method_ssh_rsa_initPEM(LIBSSH2_SESSION *session, const unsigned char *passphrase, void **abstract) { - libssh2_rsa_ctx *rsactx; + ssh2_rsa_ctx *rsactx; int ret; if(*abstract) { @@ -145,7 +144,7 @@ static int hostkey_method_ssh_rsa_initPEM(LIBSSH2_SESSION *session, *abstract = NULL; } - ret = _libssh2_rsa_new_private(&rsactx, session, privkeyfile, passphrase); + ret = ssh2_rsa_new_private(&rsactx, session, privkeyfile, passphrase); if(ret) { return -1; } @@ -165,7 +164,7 @@ static int hostkey_method_ssh_rsa_initPEMFromMemory( const unsigned char *passphrase, void **abstract) { - libssh2_rsa_ctx *rsactx; + ssh2_rsa_ctx *rsactx; int ret; if(*abstract) { @@ -173,9 +172,9 @@ static int hostkey_method_ssh_rsa_initPEMFromMemory( *abstract = NULL; } - ret = _libssh2_rsa_new_private_frommemory(&rsactx, session, - privkeyfiledata, - privkeyfiledata_len, passphrase); + ret = ssh2_rsa_new_private_frommemory(&rsactx, session, + privkeyfiledata, + privkeyfiledata_len, passphrase); if(ret) { return -1; } @@ -195,7 +194,7 @@ static int hostkey_method_ssh_rsa_sig_verify(LIBSSH2_SESSION *session, const unsigned char *m, size_t m_len, void **abstract) { - libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *)(*abstract); + ssh2_rsa_ctx *rsactx = (ssh2_rsa_ctx *)(*abstract); (void)session; /* Skip past keyname_len(4) + keyname(7){"ssh-rsa"} + signature_len(4) */ @@ -204,7 +203,7 @@ static int hostkey_method_ssh_rsa_sig_verify(LIBSSH2_SESSION *session, sig += 15; sig_len -= 15; - return _libssh2_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len); + return ssh2_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len); } /* @@ -217,32 +216,31 @@ static int hostkey_method_ssh_rsa_signv(LIBSSH2_SESSION *session, const struct iovec datavec[], void **abstract) { - libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *)(*abstract); + ssh2_rsa_ctx *rsactx = (ssh2_rsa_ctx *)(*abstract); -#ifdef _libssh2_rsa_sha1_signv - return _libssh2_rsa_sha1_signv(session, signature, signature_len, - veccount, datavec, rsactx); +#ifdef ssh2_rsa_sha1_signv + return ssh2_rsa_sha1_signv(session, signature, signature_len, + veccount, datavec, rsactx); #else int ret; int i; unsigned char hash[SHA_DIGEST_LENGTH]; - libssh2_sha1_ctx ctx; + ssh2_sha1_ctx ctx; - if(!libssh2_sha1_init(&ctx)) { + if(!ssh2_sha1_init(&ctx)) { return -1; } for(i = 0; i < veccount; i++) { - if(!libssh2_sha1_update(ctx, - datavec[i].iov_base, datavec[i].iov_len)) { + if(!ssh2_sha1_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) { return -1; } } - if(!libssh2_sha1_final(ctx, hash)) { + if(!ssh2_sha1_final(ctx, hash)) { return -1; } - ret = _libssh2_rsa_sha1_sign(session, rsactx, hash, SHA_DIGEST_LENGTH, - signature, signature_len); + ret = ssh2_rsa_sha1_sign(session, rsactx, hash, SHA_DIGEST_LENGTH, + signature, signature_len); if(ret) { return -1; } @@ -264,7 +262,7 @@ static int hostkey_method_ssh_rsa_sha2_256_sig_verify( const unsigned char *m, size_t m_len, void **abstract) { - libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *)(*abstract); + ssh2_rsa_ctx *rsactx = (ssh2_rsa_ctx *)(*abstract); (void)session; /* Skip past keyname_len(4) + keyname(12){"rsa-sha2-256"} + @@ -274,8 +272,8 @@ static int hostkey_method_ssh_rsa_sha2_256_sig_verify( sig += 20; sig_len -= 20; - return _libssh2_rsa_sha2_verify(rsactx, SHA256_DIGEST_LENGTH, sig, sig_len, - m, m_len); + return ssh2_rsa_sha2_verify(rsactx, SHA256_DIGEST_LENGTH, sig, sig_len, + m, m_len); } /* @@ -288,32 +286,31 @@ static int hostkey_method_ssh_rsa_sha2_256_signv(LIBSSH2_SESSION *session, const struct iovec datavec[], void **abstract) { - libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *)(*abstract); + ssh2_rsa_ctx *rsactx = (ssh2_rsa_ctx *)(*abstract); -#ifdef _libssh2_rsa_sha2_256_signv - return _libssh2_rsa_sha2_256_signv(session, signature, signature_len, - veccount, datavec, rsactx); +#ifdef ssh2_rsa_sha2_256_signv + return ssh2_rsa_sha2_256_signv(session, signature, signature_len, + veccount, datavec, rsactx); #else int ret; int i; unsigned char hash[SHA256_DIGEST_LENGTH]; - libssh2_sha256_ctx ctx; + ssh2_sha256_ctx ctx; - if(!libssh2_sha256_init(&ctx)) { + if(!ssh2_sha256_init(&ctx)) { return -1; } for(i = 0; i < veccount; i++) { - if(!libssh2_sha256_update(ctx, - datavec[i].iov_base, datavec[i].iov_len)) { + if(!ssh2_sha256_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) { return -1; } } - if(!libssh2_sha256_final(ctx, hash)) { + if(!ssh2_sha256_final(ctx, hash)) { return -1; } - ret = _libssh2_rsa_sha2_sign(session, rsactx, hash, SHA256_DIGEST_LENGTH, - signature, signature_len); + ret = ssh2_rsa_sha2_sign(session, rsactx, hash, SHA256_DIGEST_LENGTH, + signature, signature_len); if(ret) { return -1; } @@ -332,7 +329,7 @@ static int hostkey_method_ssh_rsa_sha2_512_sig_verify( const unsigned char *m, size_t m_len, void **abstract) { - libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *)(*abstract); + ssh2_rsa_ctx *rsactx = (ssh2_rsa_ctx *)(*abstract); (void)session; /* Skip past keyname_len(4) + keyname(12){"rsa-sha2-512"} + @@ -342,8 +339,8 @@ static int hostkey_method_ssh_rsa_sha2_512_sig_verify( sig += 20; sig_len -= 20; - return _libssh2_rsa_sha2_verify(rsactx, SHA512_DIGEST_LENGTH, sig, - sig_len, m, m_len); + return ssh2_rsa_sha2_verify(rsactx, SHA512_DIGEST_LENGTH, sig, + sig_len, m, m_len); } /* @@ -356,32 +353,31 @@ static int hostkey_method_ssh_rsa_sha2_512_signv(LIBSSH2_SESSION *session, const struct iovec datavec[], void **abstract) { - libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *)(*abstract); + ssh2_rsa_ctx *rsactx = (ssh2_rsa_ctx *)(*abstract); -#ifdef _libssh2_rsa_sha2_512_signv - return _libssh2_rsa_sha2_512_signv(session, signature, signature_len, - veccount, datavec, rsactx); +#ifdef ssh2_rsa_sha2_512_signv + return ssh2_rsa_sha2_512_signv(session, signature, signature_len, + veccount, datavec, rsactx); #else int ret; int i; unsigned char hash[SHA512_DIGEST_LENGTH]; - libssh2_sha512_ctx ctx; + ssh2_sha512_ctx ctx; - if(!libssh2_sha512_init(&ctx)) { + if(!ssh2_sha512_init(&ctx)) { return -1; } for(i = 0; i < veccount; i++) { - if(!libssh2_sha512_update(ctx, - datavec[i].iov_base, datavec[i].iov_len)) { + if(!ssh2_sha512_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) { return -1; } } - if(!libssh2_sha512_final(ctx, hash)) { + if(!ssh2_sha512_final(ctx, hash)) { return -1; } - ret = _libssh2_rsa_sha2_sign(session, rsactx, hash, SHA512_DIGEST_LENGTH, - signature, signature_len); + ret = ssh2_rsa_sha2_sign(session, rsactx, hash, SHA512_DIGEST_LENGTH, + signature, signature_len); if(ret) { return -1; } @@ -398,10 +394,10 @@ static int hostkey_method_ssh_rsa_sha2_512_signv(LIBSSH2_SESSION *session, static int hostkey_method_ssh_rsa_dtor(LIBSSH2_SESSION *session, void **abstract) { - libssh2_rsa_ctx *rsactx = (libssh2_rsa_ctx *)(*abstract); + ssh2_rsa_ctx *rsactx = (ssh2_rsa_ctx *)(*abstract); (void)session; - _libssh2_rsa_free(rsactx); + ssh2_rsa_free(rsactx); *abstract = NULL; @@ -514,7 +510,7 @@ static int hostkey_method_ssh_dss_init(LIBSSH2_SESSION *session, size_t hostkey_data_len, void **abstract) { - libssh2_dsa_ctx *dsactx; + ssh2_dsa_ctx *dsactx; unsigned char *p, *q, *g, *y; size_t p_len, q_len, g_len, y_len; struct string_buf buf; @@ -525,39 +521,38 @@ static int hostkey_method_ssh_dss_init(LIBSSH2_SESSION *session, } if(hostkey_data_len < 27) { - _libssh2_debug((session, LIBSSH2_TRACE_ERROR, - "host key length too short")); + ssh2_deb((session, LIBSSH2_TRACE_ERROR, "host key length too short")); return -1; } - buf.data = (unsigned char *)LIBSSH2_UNCONST(hostkey_data); + buf.data = (unsigned char *)SSH2_UNCONST(hostkey_data); buf.dataptr = buf.data; buf.len = hostkey_data_len; - if(_libssh2_match_string(&buf, "ssh-dss")) + if(ssh2_match_string(&buf, "ssh-dss")) return -1; - if(_libssh2_get_string(&buf, &p, &p_len)) + if(ssh2_get_string(&buf, &p, &p_len)) return -1; - if(_libssh2_get_string(&buf, &q, &q_len)) + if(ssh2_get_string(&buf, &q, &q_len)) return -1; - if(_libssh2_get_string(&buf, &g, &g_len)) + if(ssh2_get_string(&buf, &g, &g_len)) return -1; - if(_libssh2_get_string(&buf, &y, &y_len)) + if(ssh2_get_string(&buf, &y, &y_len)) return -1; - if(!_libssh2_eob(&buf)) + if(!ssh2_eob(&buf)) return -1; - if(_libssh2_dsa_new(&dsactx, - p, (unsigned long)p_len, - q, (unsigned long)q_len, - g, (unsigned long)g_len, - y, (unsigned long)y_len, - NULL, 0)) { + if(ssh2_dsa_new(&dsactx, + p, (unsigned long)p_len, + q, (unsigned long)q_len, + g, (unsigned long)g_len, + y, (unsigned long)y_len, + NULL, 0)) { return -1; } @@ -574,7 +569,7 @@ static int hostkey_method_ssh_dss_initPEM(LIBSSH2_SESSION *session, const unsigned char *passphrase, void **abstract) { - libssh2_dsa_ctx *dsactx; + ssh2_dsa_ctx *dsactx; int ret; if(*abstract) { @@ -582,7 +577,7 @@ static int hostkey_method_ssh_dss_initPEM(LIBSSH2_SESSION *session, *abstract = NULL; } - ret = _libssh2_dsa_new_private(&dsactx, session, privkeyfile, passphrase); + ret = ssh2_dsa_new_private(&dsactx, session, privkeyfile, passphrase); if(ret) { return -1; } @@ -602,7 +597,7 @@ static int hostkey_method_ssh_dss_initPEMFromMemory( const unsigned char *passphrase, void **abstract) { - libssh2_dsa_ctx *dsactx; + ssh2_dsa_ctx *dsactx; int ret; if(*abstract) { @@ -610,9 +605,9 @@ static int hostkey_method_ssh_dss_initPEMFromMemory( *abstract = NULL; } - ret = _libssh2_dsa_new_private_frommemory(&dsactx, session, - privkeyfiledata, - privkeyfiledata_len, passphrase); + ret = ssh2_dsa_new_private_frommemory(&dsactx, session, + privkeyfiledata, + privkeyfiledata_len, passphrase); if(ret) { return -1; } @@ -631,18 +626,18 @@ static int hostkey_method_ssh_dss_sig_verify(LIBSSH2_SESSION *session, const unsigned char *m, size_t m_len, void **abstract) { - libssh2_dsa_ctx *dsactx = (libssh2_dsa_ctx *)(*abstract); + ssh2_dsa_ctx *dsactx = (ssh2_dsa_ctx *)(*abstract); /* Skip past keyname_len(4) + keyname(7){"ssh-dss"} + signature_len(4) */ if(sig_len != 55) { - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid DSS signature length"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Invalid DSS signature length"); } sig += 15; sig_len -= 15; - return _libssh2_dsa_sha1_verify(dsactx, sig, m, m_len); + return ssh2_dsa_sha1_verify(dsactx, sig, m, m_len); } /* @@ -655,18 +650,18 @@ static int hostkey_method_ssh_dss_signv(LIBSSH2_SESSION *session, const struct iovec datavec[], void **abstract) { - libssh2_dsa_ctx *dsactx = (libssh2_dsa_ctx *)(*abstract); + ssh2_dsa_ctx *dsactx = (ssh2_dsa_ctx *)(*abstract); unsigned char hash[SHA_DIGEST_LENGTH]; - libssh2_sha1_ctx ctx; + ssh2_sha1_ctx ctx; int i; - if(!libssh2_sha1_init(&ctx)) { + if(!ssh2_sha1_init(&ctx)) { *signature = NULL; *signature_len = 0; return -1; } - *signature = LIBSSH2_CALLOC(session, 2 * SHA_DIGEST_LENGTH); + *signature = SSH2_CALLOC(session, 2 * SHA_DIGEST_LENGTH); if(!*signature) { return -1; } @@ -674,17 +669,16 @@ static int hostkey_method_ssh_dss_signv(LIBSSH2_SESSION *session, *signature_len = 2 * SHA_DIGEST_LENGTH; for(i = 0; i < veccount; i++) { - if(!libssh2_sha1_update(ctx, - datavec[i].iov_base, datavec[i].iov_len)) { + if(!ssh2_sha1_update(ctx, datavec[i].iov_base, datavec[i].iov_len)) { return -1; } } - if(!libssh2_sha1_final(ctx, hash)) { + if(!ssh2_sha1_final(ctx, hash)) { return -1; } - if(_libssh2_dsa_sha1_sign(dsactx, hash, SHA_DIGEST_LENGTH, *signature)) { - LIBSSH2_FREE(session, *signature); + if(ssh2_dsa_sha1_sign(dsactx, hash, SHA_DIGEST_LENGTH, *signature)) { + SSH2_FREE(session, *signature); return -1; } @@ -697,10 +691,10 @@ static int hostkey_method_ssh_dss_signv(LIBSSH2_SESSION *session, static int hostkey_method_ssh_dss_dtor(LIBSSH2_SESSION *session, void **abstract) { - libssh2_dsa_ctx *dsactx = (libssh2_dsa_ctx *)(*abstract); + ssh2_dsa_ctx *dsactx = (ssh2_dsa_ctx *)(*abstract); (void)session; - _libssh2_dsa_free(dsactx); + ssh2_dsa_free(dsactx); *abstract = NULL; @@ -737,10 +731,10 @@ static int hostkey_method_ssh_ecdsa_init(LIBSSH2_SESSION *session, size_t hostkey_data_len, void **abstract) { - libssh2_ecdsa_ctx *ec_ctx = NULL; + ssh2_ecdsa_ctx *ec_ctx = NULL; unsigned char *type_str, *domain, *public_key; size_t key_len, len; - libssh2_curve_type type; + ssh2_curve_type type; struct string_buf buf; if(abstract && *abstract) { @@ -749,56 +743,55 @@ static int hostkey_method_ssh_ecdsa_init(LIBSSH2_SESSION *session, } if(hostkey_data_len < 39) { - _libssh2_debug((session, LIBSSH2_TRACE_ERROR, - "host key length too short")); + ssh2_deb((session, LIBSSH2_TRACE_ERROR, "host key length too short")); return -1; } - buf.data = (unsigned char *)LIBSSH2_UNCONST(hostkey_data); + buf.data = (unsigned char *)SSH2_UNCONST(hostkey_data); buf.dataptr = buf.data; buf.len = hostkey_data_len; - if(_libssh2_get_string(&buf, &type_str, &len) || len != 19) + if(ssh2_get_string(&buf, &type_str, &len) || len != 19) return -1; if(strncmp((char *)type_str, "ecdsa-sha2-nistp256", 19) == 0) { - type = LIBSSH2_EC_CURVE_NISTP256; + type = SSH2_EC_CURVE_NISTP256; } else if(strncmp((char *)type_str, "ecdsa-sha2-nistp384", 19) == 0) { - type = LIBSSH2_EC_CURVE_NISTP384; + type = SSH2_EC_CURVE_NISTP384; } else if(strncmp((char *)type_str, "ecdsa-sha2-nistp521", 19) == 0) { - type = LIBSSH2_EC_CURVE_NISTP521; + type = SSH2_EC_CURVE_NISTP521; } else { return -1; } - if(_libssh2_get_string(&buf, &domain, &len) || len != 8) + if(ssh2_get_string(&buf, &domain, &len) || len != 8) return -1; - if(type == LIBSSH2_EC_CURVE_NISTP256 && + if(type == SSH2_EC_CURVE_NISTP256 && strncmp((char *)domain, "nistp256", 8) != 0) { return -1; } - else if(type == LIBSSH2_EC_CURVE_NISTP384 && + else if(type == SSH2_EC_CURVE_NISTP384 && strncmp((char *)domain, "nistp384", 8) != 0) { return -1; } - else if(type == LIBSSH2_EC_CURVE_NISTP521 && + else if(type == SSH2_EC_CURVE_NISTP521 && strncmp((char *)domain, "nistp521", 8) != 0) { return -1; } /* public key */ - if(_libssh2_get_string(&buf, &public_key, &key_len)) + if(ssh2_get_string(&buf, &public_key, &key_len)) return -1; - if(!_libssh2_eob(&buf)) + if(!ssh2_eob(&buf)) return -1; - if(_libssh2_ecdsa_curve_name_with_octal_new(&ec_ctx, public_key, - key_len, type)) + if(ssh2_ecdsa_curve_name_with_octal_new(&ec_ctx, public_key, + key_len, type)) return -1; if(abstract) @@ -815,7 +808,7 @@ static int hostkey_method_ssh_ecdsa_initPEM(LIBSSH2_SESSION *session, const unsigned char *passphrase, void **abstract) { - libssh2_ecdsa_ctx *ec_ctx = NULL; + ssh2_ecdsa_ctx *ec_ctx = NULL; int ret; if(abstract && *abstract) { @@ -823,8 +816,7 @@ static int hostkey_method_ssh_ecdsa_initPEM(LIBSSH2_SESSION *session, *abstract = NULL; } - ret = _libssh2_ecdsa_new_private(&ec_ctx, session, - privkeyfile, passphrase); + ret = ssh2_ecdsa_new_private(&ec_ctx, session, privkeyfile, passphrase); if(abstract) *abstract = ec_ctx; @@ -842,7 +834,7 @@ static int hostkey_method_ssh_ecdsa_initPEMFromMemory( const unsigned char *passphrase, void **abstract) { - libssh2_ecdsa_ctx *ec_ctx = NULL; + ssh2_ecdsa_ctx *ec_ctx = NULL; int ret; if(abstract && *abstract) { @@ -850,10 +842,10 @@ static int hostkey_method_ssh_ecdsa_initPEMFromMemory( *abstract = NULL; } - ret = _libssh2_ecdsa_new_private_frommemory(&ec_ctx, session, - privkeyfiledata, - privkeyfiledata_len, - passphrase); + ret = ssh2_ecdsa_new_private_frommemory(&ec_ctx, session, + privkeyfiledata, + privkeyfiledata_len, + passphrase); if(ret) { return -1; } @@ -877,7 +869,7 @@ static int hostkey_method_ssh_ecdsa_sig_verify(LIBSSH2_SESSION *session, size_t r_len, s_len, name_len; uint32_t len; struct string_buf buf; - libssh2_ecdsa_ctx *ctx = (libssh2_ecdsa_ctx *)(*abstract); + ssh2_ecdsa_ctx *ctx = (ssh2_ecdsa_ctx *)(*abstract); (void)session; @@ -886,52 +878,52 @@ static int hostkey_method_ssh_ecdsa_sig_verify(LIBSSH2_SESSION *session, /* keyname_len(4) + keyname(19){"ecdsa-sha2-nistp256"} + signature_len(4) */ - buf.data = (unsigned char *)LIBSSH2_UNCONST(sig); + buf.data = (unsigned char *)SSH2_UNCONST(sig); buf.dataptr = buf.data; buf.len = sig_len; - if(_libssh2_get_string(&buf, &name, &name_len) || name_len != 19) + if(ssh2_get_string(&buf, &name, &name_len) || name_len != 19) return -1; - if(_libssh2_get_u32(&buf, &len) != 0 || len < 8) + if(ssh2_get_u32(&buf, &len) != 0 || len < 8) return -1; - if(_libssh2_get_string(&buf, &r, &r_len)) + if(ssh2_get_string(&buf, &r, &r_len)) return -1; - if(_libssh2_get_string(&buf, &s, &s_len)) + if(ssh2_get_string(&buf, &s, &s_len)) return -1; - return _libssh2_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len); + return ssh2_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len); } -#define LIBSSH2_HOSTKEY_METHOD_EC_SIGNV_HASH(digest_type) \ - do { \ - unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \ - libssh2_sha##digest_type##_ctx ctx; \ - int i; \ - if(!libssh2_sha##digest_type##_init(&ctx)) { \ - ret = -1; \ - break; \ - } \ - for(i = 0; i < veccount; i++) { \ - if(!libssh2_sha##digest_type##_update(ctx, \ - datavec[i].iov_base, \ - datavec[i].iov_len)) { \ - ret = -1; \ - break; \ - } \ - } \ - if(ret == -1) { \ - break; \ - } \ - if(!libssh2_sha##digest_type##_final(ctx, hash)) { \ - ret = -1; \ - break; \ - } \ - ret = _libssh2_ecdsa_sign(session, ec_ctx, hash, \ - SHA##digest_type##_DIGEST_LENGTH, \ - signature, signature_len); \ +#define HOSTKEY_METHOD_EC_SIGNV_HASH(digest_type) \ + do { \ + unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \ + ssh2_sha##digest_type##_ctx ctx; \ + int i; \ + if(!ssh2_sha##digest_type##_init(&ctx)) { \ + ret = -1; \ + break; \ + } \ + for(i = 0; i < veccount; i++) { \ + if(!ssh2_sha##digest_type##_update(ctx, \ + datavec[i].iov_base, \ + datavec[i].iov_len)) { \ + ret = -1; \ + break; \ + } \ + } \ + if(ret == -1) { \ + break; \ + } \ + if(!ssh2_sha##digest_type##_final(ctx, hash)) { \ + ret = -1; \ + break; \ + } \ + ret = ssh2_ecdsa_sign(session, ec_ctx, hash, \ + SHA##digest_type##_DIGEST_LENGTH, \ + signature, signature_len); \ } while(0) /* @@ -944,18 +936,18 @@ static int hostkey_method_ssh_ecdsa_signv(LIBSSH2_SESSION *session, const struct iovec datavec[], void **abstract) { - libssh2_ecdsa_ctx *ec_ctx = (libssh2_ecdsa_ctx *)(*abstract); - libssh2_curve_type type = _libssh2_ecdsa_get_curve_type(ec_ctx); + ssh2_ecdsa_ctx *ec_ctx = (ssh2_ecdsa_ctx *)(*abstract); + ssh2_curve_type type = ssh2_ecdsa_get_curve_type(ec_ctx); int ret = 0; - if(type == LIBSSH2_EC_CURVE_NISTP256) { - LIBSSH2_HOSTKEY_METHOD_EC_SIGNV_HASH(256); + if(type == SSH2_EC_CURVE_NISTP256) { + HOSTKEY_METHOD_EC_SIGNV_HASH(256); } - else if(type == LIBSSH2_EC_CURVE_NISTP384) { - LIBSSH2_HOSTKEY_METHOD_EC_SIGNV_HASH(384); + else if(type == SSH2_EC_CURVE_NISTP384) { + HOSTKEY_METHOD_EC_SIGNV_HASH(384); } - else if(type == LIBSSH2_EC_CURVE_NISTP521) { - LIBSSH2_HOSTKEY_METHOD_EC_SIGNV_HASH(512); + else if(type == SSH2_EC_CURVE_NISTP521) { + HOSTKEY_METHOD_EC_SIGNV_HASH(512); } else { return -1; @@ -970,11 +962,11 @@ static int hostkey_method_ssh_ecdsa_signv(LIBSSH2_SESSION *session, static int hostkey_method_ssh_ecdsa_dtor(LIBSSH2_SESSION *session, void **abstract) { - libssh2_ecdsa_ctx *keyctx = (libssh2_ecdsa_ctx *)(*abstract); + ssh2_ecdsa_ctx *keyctx = (ssh2_ecdsa_ctx *)(*abstract); (void)session; if(keyctx) - _libssh2_ecdsa_free(keyctx); + ssh2_ecdsa_free(keyctx); *abstract = NULL; @@ -1074,7 +1066,7 @@ static int hostkey_method_ssh_ed25519_init(LIBSSH2_SESSION *session, { size_t key_len; unsigned char *key; - libssh2_ed25519_ctx *ctx = NULL; + ssh2_ed25519_ctx *ctx = NULL; struct string_buf buf; if(*abstract) { @@ -1083,26 +1075,25 @@ static int hostkey_method_ssh_ed25519_init(LIBSSH2_SESSION *session, } if(hostkey_data_len < 19) { - _libssh2_debug((session, LIBSSH2_TRACE_ERROR, - "host key length too short")); + ssh2_deb((session, LIBSSH2_TRACE_ERROR, "host key length too short")); return -1; } - buf.data = (unsigned char *)LIBSSH2_UNCONST(hostkey_data); + buf.data = (unsigned char *)SSH2_UNCONST(hostkey_data); buf.dataptr = buf.data; buf.len = hostkey_data_len; - if(_libssh2_match_string(&buf, "ssh-ed25519")) + if(ssh2_match_string(&buf, "ssh-ed25519")) return -1; /* public key */ - if(_libssh2_get_string(&buf, &key, &key_len)) + if(ssh2_get_string(&buf, &key, &key_len)) return -1; - if(!_libssh2_eob(&buf)) + if(!ssh2_eob(&buf)) return -1; - if(_libssh2_ed25519_new_public(&ctx, session, key, key_len) != 0) { + if(ssh2_ed25519_new_public(&ctx, session, key, key_len) != 0) { return -1; } @@ -1114,13 +1105,12 @@ static int hostkey_method_ssh_ed25519_init(LIBSSH2_SESSION *session, /* * Load a Private Key from a PEM file */ -static int hostkey_method_ssh_ed25519_initPEM( - LIBSSH2_SESSION *session, - const char *privkeyfile, - const unsigned char *passphrase, - void **abstract) +static int hostkey_method_ssh_ed25519_initPEM(LIBSSH2_SESSION *session, + const char *privkeyfile, + const unsigned char *passphrase, + void **abstract) { - libssh2_ed25519_ctx *ec_ctx = NULL; + ssh2_ed25519_ctx *ec_ctx = NULL; int ret; if(*abstract) { @@ -1128,8 +1118,7 @@ static int hostkey_method_ssh_ed25519_initPEM( *abstract = NULL; } - ret = _libssh2_ed25519_new_private(&ec_ctx, session, - privkeyfile, passphrase); + ret = ssh2_ed25519_new_private(&ec_ctx, session, privkeyfile, passphrase); if(ret) { return -1; } @@ -1149,7 +1138,7 @@ static int hostkey_method_ssh_ed25519_initPEMFromMemory( const unsigned char *passphrase, void **abstract) { - libssh2_ed25519_ctx *ed_ctx = NULL; + ssh2_ed25519_ctx *ed_ctx = NULL; int ret; if(abstract && *abstract) { @@ -1157,10 +1146,10 @@ static int hostkey_method_ssh_ed25519_initPEMFromMemory( *abstract = NULL; } - ret = _libssh2_ed25519_new_private_frommemory(&ed_ctx, session, - privkeyfiledata, - privkeyfiledata_len, - passphrase); + ret = ssh2_ed25519_new_private_frommemory(&ed_ctx, session, + privkeyfiledata, + privkeyfiledata_len, + passphrase); if(ret) { return -1; } @@ -1180,7 +1169,7 @@ static int hostkey_method_ssh_ed25519_sig_verify(LIBSSH2_SESSION *session, const unsigned char *m, size_t m_len, void **abstract) { - libssh2_ed25519_ctx *ctx = (libssh2_ed25519_ctx *)(*abstract); + ssh2_ed25519_ctx *ctx = (ssh2_ed25519_ctx *)(*abstract); (void)session; if(sig_len < 19) @@ -1191,10 +1180,10 @@ static int hostkey_method_ssh_ed25519_sig_verify(LIBSSH2_SESSION *session, sig += 19; sig_len -= 19; - if(sig_len != LIBSSH2_ED25519_SIG_LEN) + if(sig_len != SSH2_ED25519_SIG_LEN) return -1; - return _libssh2_ed25519_verify(ctx, sig, sig_len, m, m_len); + return ssh2_ed25519_verify(ctx, sig, sig_len, m, m_len); } /* @@ -1207,15 +1196,15 @@ static int hostkey_method_ssh_ed25519_signv(LIBSSH2_SESSION *session, const struct iovec datavec[], void **abstract) { - libssh2_ed25519_ctx *ctx = (libssh2_ed25519_ctx *)(*abstract); + ssh2_ed25519_ctx *ctx = (ssh2_ed25519_ctx *)(*abstract); if(veccount != 1) { return -1; } - return _libssh2_ed25519_sign(ctx, session, signature, signature_len, - (const uint8_t *)datavec[0].iov_base, - datavec[0].iov_len); + return ssh2_ed25519_sign(ctx, session, signature, signature_len, + (const uint8_t *)datavec[0].iov_base, + datavec[0].iov_len); } /* @@ -1224,11 +1213,11 @@ static int hostkey_method_ssh_ed25519_signv(LIBSSH2_SESSION *session, static int hostkey_method_ssh_ed25519_dtor(LIBSSH2_SESSION *session, void **abstract) { - libssh2_ed25519_ctx *keyctx = (libssh2_ed25519_ctx *)(*abstract); + ssh2_ed25519_ctx *keyctx = (ssh2_ed25519_ctx *)(*abstract); (void)session; if(keyctx) - _libssh2_ed25519_free(keyctx); + ssh2_ed25519_free(keyctx); *abstract = NULL; @@ -1303,7 +1292,7 @@ static const struct hostkey_method *hostkey_methods[] = { NULL }; -const struct hostkey_method **libssh2_hostkey_methods(void) +const struct hostkey_method **ssh2_hostkey_methods(void) { return hostkey_methods; } diff --git a/src/keepalive.c b/src/keepalive.c index 82e6c509..cf629288 100644 --- a/src/keepalive.c +++ b/src/keepalive.c @@ -38,7 +38,7 @@ */ #include "libssh2_priv.h" -#include "transport.h" /* _libssh2_transport_write */ +#include "transport.h" /* ssh2_transport_write() */ /* Keep-alive stuff. */ @@ -77,12 +77,12 @@ int libssh2_keepalive_send(LIBSSH2_SESSION *session, int *seconds_to_next) keepalive_data[len - 1] = (unsigned char)session->keepalive_want_reply; - rc = _libssh2_transport_send(session, keepalive_data, len, NULL, 0); + rc = ssh2_transport_send(session, keepalive_data, len, NULL, 0); /* Silently ignore PACKET_EAGAIN here: if the write buffer is already full, sending another keepalive is not useful. */ if(rc && rc != LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send keepalive message"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send keepalive message"); return rc; } diff --git a/src/kex.c b/src/kex.c index 22d4de96..6138378a 100644 --- a/src/kex.c +++ b/src/kex.c @@ -55,50 +55,47 @@ /* Helper macro called from kex_method_diffie_hellman_group1_sha1_key_exchange */ -#define LIBSSH2_KEX_METHOD_SHA_VALUE_HASH(digest_type, value, \ - reqlen, version) \ +#define KEX_METHOD_SHA_VALUE_HASH(digest_type, value, reqlen, version) \ do { \ - libssh2_sha##digest_type##_ctx hash; \ + ssh2_sha##digest_type##_ctx hash; \ size_t len = 0; \ if(!(value)) { \ - (value) = LIBSSH2_ALLOC(session, \ - (reqlen) + \ - SHA##digest_type##_DIGEST_LENGTH); \ + (value) = SSH2_ALLOC(session, \ + (reqlen) + \ + SHA##digest_type##_DIGEST_LENGTH); \ } \ if(value) \ while(len < (size_t)(reqlen)) { \ - if(!libssh2_sha##digest_type##_init(&hash) || \ - !libssh2_sha##digest_type##_update(hash, \ + if(!ssh2_sha##digest_type##_init(&hash) || \ + !ssh2_sha##digest_type##_update(hash, \ exchange_state->k_value, \ exchange_state->k_value_len) || \ - !libssh2_sha##digest_type##_update(hash, \ + !ssh2_sha##digest_type##_update(hash, \ exchange_state->h_sig_comp, \ SHA##digest_type##_DIGEST_LENGTH)) { \ - LIBSSH2_FREE(session, value); \ + SSH2_FREE(session, value); \ (value) = NULL; \ break; \ } \ if(len > 0) { \ - if(!libssh2_sha##digest_type##_update(hash, \ - value, len)) { \ - LIBSSH2_FREE(session, value); \ + if(!ssh2_sha##digest_type##_update(hash, value, len)) { \ + SSH2_FREE(session, value); \ (value) = NULL; \ break; \ } \ } \ else { \ - if(!libssh2_sha##digest_type##_update(hash, \ - version, 1) || \ - !libssh2_sha##digest_type##_update(hash, \ + if(!ssh2_sha##digest_type##_update(hash, version, 1) || \ + !ssh2_sha##digest_type##_update(hash, \ session->session_id, \ session->session_id_len)) { \ - LIBSSH2_FREE(session, value); \ + SSH2_FREE(session, value); \ (value) = NULL; \ break; \ } \ } \ - if(!libssh2_sha##digest_type##_final(hash, (value) + len)) { \ - LIBSSH2_FREE(session, value); \ + if(!ssh2_sha##digest_type##_final(hash, (value) + len)) { \ + SSH2_FREE(session, value); \ (value) = NULL; \ break; \ } \ @@ -116,16 +113,16 @@ static int sha_algo_ctx_init(int sha_algo, void *ctx) { if(sha_algo == 512) { - return libssh2_sha512_init((libssh2_sha512_ctx *)ctx); + return ssh2_sha512_init((ssh2_sha512_ctx *)ctx); } else if(sha_algo == 384) { - return libssh2_sha384_init((libssh2_sha384_ctx *)ctx); + return ssh2_sha384_init((ssh2_sha384_ctx *)ctx); } else if(sha_algo == 256) { - return libssh2_sha256_init((libssh2_sha256_ctx *)ctx); + return ssh2_sha256_init((ssh2_sha256_ctx *)ctx); } else if(sha_algo == 1) { - return libssh2_sha1_init((libssh2_sha1_ctx *)ctx); + return ssh2_sha1_init((ssh2_sha1_ctx *)ctx); } else { #ifdef LIBSSH2DEBUG @@ -139,20 +136,20 @@ static int sha_algo_ctx_update(int sha_algo, void *ctx, const void *data, size_t len) { if(sha_algo == 512) { - libssh2_sha512_ctx *_ctx = (libssh2_sha512_ctx *)ctx; - return libssh2_sha512_update(*_ctx, data, len); + ssh2_sha512_ctx *_ctx = (ssh2_sha512_ctx *)ctx; + return ssh2_sha512_update(*_ctx, data, len); } else if(sha_algo == 384) { - libssh2_sha384_ctx *_ctx = (libssh2_sha384_ctx *)ctx; - return libssh2_sha384_update(*_ctx, data, len); + ssh2_sha384_ctx *_ctx = (ssh2_sha384_ctx *)ctx; + return ssh2_sha384_update(*_ctx, data, len); } else if(sha_algo == 256) { - libssh2_sha256_ctx *_ctx = (libssh2_sha256_ctx *)ctx; - return libssh2_sha256_update(*_ctx, data, len); + ssh2_sha256_ctx *_ctx = (ssh2_sha256_ctx *)ctx; + return ssh2_sha256_update(*_ctx, data, len); } else if(sha_algo == 1) { - libssh2_sha1_ctx *_ctx = (libssh2_sha1_ctx *)ctx; - return libssh2_sha1_update(*_ctx, data, len); + ssh2_sha1_ctx *_ctx = (ssh2_sha1_ctx *)ctx; + return ssh2_sha1_update(*_ctx, data, len); } else { #ifdef LIBSSH2DEBUG @@ -165,20 +162,20 @@ static int sha_algo_ctx_update(int sha_algo, void *ctx, static int sha_algo_ctx_final(int sha_algo, void *ctx, void *hash) { if(sha_algo == 512) { - libssh2_sha512_ctx *_ctx = (libssh2_sha512_ctx *)ctx; - return libssh2_sha512_final(*_ctx, hash); + ssh2_sha512_ctx *_ctx = (ssh2_sha512_ctx *)ctx; + return ssh2_sha512_final(*_ctx, hash); } else if(sha_algo == 384) { - libssh2_sha384_ctx *_ctx = (libssh2_sha384_ctx *)ctx; - return libssh2_sha384_final(*_ctx, hash); + ssh2_sha384_ctx *_ctx = (ssh2_sha384_ctx *)ctx; + return ssh2_sha384_final(*_ctx, hash); } else if(sha_algo == 256) { - libssh2_sha256_ctx *_ctx = (libssh2_sha256_ctx *)ctx; - return libssh2_sha256_final(*_ctx, hash); + ssh2_sha256_ctx *_ctx = (ssh2_sha256_ctx *)ctx; + return ssh2_sha256_final(*_ctx, hash); } else if(sha_algo == 1) { - libssh2_sha1_ctx *_ctx = (libssh2_sha1_ctx *)ctx; - return libssh2_sha1_final(*_ctx, hash); + ssh2_sha1_ctx *_ctx = (ssh2_sha1_ctx *)ctx; + return ssh2_sha1_final(*_ctx, hash); } else { #ifdef LIBSSH2DEBUG @@ -195,16 +192,16 @@ static void sha_algo_value_hash(int sha_algo, const unsigned char *version) { if(sha_algo == 512) { - LIBSSH2_KEX_METHOD_SHA_VALUE_HASH(512, *data, data_len, version); + KEX_METHOD_SHA_VALUE_HASH(512, *data, data_len, version); } else if(sha_algo == 384) { - LIBSSH2_KEX_METHOD_SHA_VALUE_HASH(384, *data, data_len, version); + KEX_METHOD_SHA_VALUE_HASH(384, *data, data_len, version); } else if(sha_algo == 256) { - LIBSSH2_KEX_METHOD_SHA_VALUE_HASH(256, *data, data_len, version); + KEX_METHOD_SHA_VALUE_HASH(256, *data, data_len, version); } else if(sha_algo == 1) { - LIBSSH2_KEX_METHOD_SHA_VALUE_HASH(1, *data, data_len, version); + KEX_METHOD_SHA_VALUE_HASH(1, *data, data_len, version); } else { #ifdef LIBSSH2DEBUG @@ -223,16 +220,16 @@ static int process_host_key(LIBSSH2_SESSION *session, /* Parse reply */ if(data) { if(data_len < 5) { - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet length"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected packet length"); } buf->data = data; buf->len = data_len; } else { if(exchange_state->s_packet_len < 5) { - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet length"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected packet length"); } buf->data = exchange_state->s_packet; buf->len = exchange_state->s_packet_len; @@ -242,28 +239,27 @@ static int process_host_key(LIBSSH2_SESSION *session, buf->dataptr++; /* advance past type */ if(session->server_hostkey) { - LIBSSH2_FREE(session, session->server_hostkey); + SSH2_FREE(session, session->server_hostkey); session->server_hostkey = NULL; session->server_hostkey_len = 0; } - if(_libssh2_copy_string(session, buf, &(session->server_hostkey), - &host_key_len)) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Could not copy host key"); + if(ssh2_copy_string(session, buf, &(session->server_hostkey), + &host_key_len)) { + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Could not copy host key"); } session->server_hostkey_len = (uint32_t)host_key_len; #if LIBSSH2_MD5 { - libssh2_md5_ctx fingerprint_ctx; + ssh2_md5_ctx fingerprint_ctx; - if(libssh2_md5_init(&fingerprint_ctx) && - libssh2_md5_update(fingerprint_ctx, session->server_hostkey, + if(ssh2_md5_init(&fingerprint_ctx) && + ssh2_md5_update(fingerprint_ctx, session->server_hostkey, session->server_hostkey_len) && - libssh2_md5_final(fingerprint_ctx, - session->server_hostkey_md5)) { + ssh2_md5_final(fingerprint_ctx, session->server_hostkey_md5)) { session->server_hostkey_md5_valid = TRUE; } else { @@ -279,19 +275,19 @@ static int process_host_key(LIBSSH2_SESSION *session, snprintf(fprint, 4, "%02x:", session->server_hostkey_md5[i]); } *(--fprint) = '\0'; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Server's MD5 Fingerprint: %s", fingerprint)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Server's MD5 Fingerprint: %s", + fingerprint)); } #endif /* LIBSSH2DEBUG */ #endif /* ! LIBSSH2_MD5 */ { - libssh2_sha1_ctx fingerprint_ctx; + ssh2_sha1_ctx fingerprint_ctx; - if(libssh2_sha1_init(&fingerprint_ctx) && - libssh2_sha1_update(fingerprint_ctx, session->server_hostkey, - session->server_hostkey_len) && - libssh2_sha1_final(fingerprint_ctx, session->server_hostkey_sha1)) { + if(ssh2_sha1_init(&fingerprint_ctx) && + ssh2_sha1_update(fingerprint_ctx, session->server_hostkey, + session->server_hostkey_len) && + ssh2_sha1_final(fingerprint_ctx, session->server_hostkey_sha1)) { session->server_hostkey_sha1_valid = TRUE; } else { @@ -307,19 +303,19 @@ static int process_host_key(LIBSSH2_SESSION *session, snprintf(fprint, 4, "%02x:", session->server_hostkey_sha1[i]); } *(--fprint) = '\0'; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Server's SHA1 Fingerprint: %s", fingerprint)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Server's SHA1 Fingerprint: %s", + fingerprint)); } #endif /* LIBSSH2DEBUG */ { - libssh2_sha256_ctx fingerprint_ctx; + ssh2_sha256_ctx fingerprint_ctx; - if(libssh2_sha256_init(&fingerprint_ctx) && - libssh2_sha256_update(fingerprint_ctx, session->server_hostkey, - session->server_hostkey_len) && - libssh2_sha256_final(fingerprint_ctx, - session->server_hostkey_sha256)) { + if(ssh2_sha256_init(&fingerprint_ctx) && + ssh2_sha256_update(fingerprint_ctx, session->server_hostkey, + session->server_hostkey_len) && + ssh2_sha256_final(fingerprint_ctx, + session->server_hostkey_sha256)) { session->server_hostkey_sha256_valid = TRUE; } else { @@ -329,26 +325,25 @@ static int process_host_key(LIBSSH2_SESSION *session, #ifdef LIBSSH2DEBUG { char *base64Fingerprint = NULL; - _libssh2_base64_encode(session, - (const char *)session->server_hostkey_sha256, - SHA256_DIGEST_LENGTH, &base64Fingerprint); + ssh2_base64_encode(session, + (const char *)session->server_hostkey_sha256, + SHA256_DIGEST_LENGTH, &base64Fingerprint); if(base64Fingerprint) { - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Server's SHA256 Fingerprint: %s", - base64Fingerprint)); - LIBSSH2_FREE(session, base64Fingerprint); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Server's SHA256 Fingerprint: %s", base64Fingerprint)); + SSH2_FREE(session, base64Fingerprint); } } #endif /* LIBSSH2DEBUG */ if(!session->hostkey) { - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, "hostkey is NULL"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, "hostkey is NULL"); } if(session->hostkey->init(session, session->server_hostkey, session->server_hostkey_len, &session->server_hostkey_abstract)) { - return _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unable to initialize hostkey importer"); + return ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unable to initialize hostkey importer"); } return 0; @@ -359,36 +354,35 @@ static int finish_kex(LIBSSH2_SESSION *session, int digest_len, int sha_algo_value) { int rc; - rc = _libssh2_packet_require(session, SSH_MSG_NEWKEYS, - &exchange_state->tmp, - &exchange_state->tmp_len, 0, NULL, 0, - &exchange_state->req_state); + rc = ssh2_packet_require(session, SSH_MSG_NEWKEYS, + &exchange_state->tmp, + &exchange_state->tmp_len, 0, NULL, 0, + &exchange_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if(rc) { - return _libssh2_error(session, rc, "Timed out waiting for NEWKEYS"); + return ssh2_err(session, rc, "Timed out waiting for NEWKEYS"); } /* The first key exchange has been performed, switch to active crypt/comp/mac mode */ - session->state |= LIBSSH2_STATE_NEWKEYS; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, "Received NEWKEYS message")); + session->state |= SSH2_STATE_NEWKEYS; + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Received NEWKEYS message")); /* This actually ends up being packet_type(1) for this packet type anyway */ - LIBSSH2_FREE(session, exchange_state->tmp); + SSH2_FREE(session, exchange_state->tmp); if(!session->session_id) { - session->session_id = LIBSSH2_ALLOC(session, digest_len); + session->session_id = SSH2_ALLOC(session, digest_len); if(!session->session_id) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate buffer for " - "SHA digest"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate buffer for SHA digest"); } memcpy(session->session_id, exchange_state->h_sig_comp, digest_len); session->session_id_len = digest_len; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, "session_id calculated")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "session_id calculated")); } /* Cleanup any existing cipher */ @@ -415,29 +409,29 @@ static int finish_kex(LIBSSH2_SESSION *session, (const unsigned char *)"C"); if(!secret) { - LIBSSH2_FREE(session, iv); + SSH2_FREE(session, iv); return LIBSSH2_ERROR_KEX_FAILURE; } - if(session->local.crypt-> - init(session, session->local.crypt, iv, &free_iv, secret, - &free_secret, 1, &session->local.crypt_abstract)) { - LIBSSH2_FREE(session, iv); - LIBSSH2_FREE(session, secret); + if(session->local.crypt->init(session, session->local.crypt, iv, + &free_iv, secret, &free_secret, 1, + &session->local.crypt_abstract)) { + SSH2_FREE(session, iv); + SSH2_FREE(session, secret); return LIBSSH2_ERROR_KEX_FAILURE; } if(free_iv) { - _libssh2_explicit_zero(iv, session->local.crypt->iv_len); - LIBSSH2_FREE(session, iv); + ssh2_explicit_zero(iv, session->local.crypt->iv_len); + SSH2_FREE(session, iv); } if(free_secret) { - _libssh2_explicit_zero(secret, session->local.crypt->secret_len); - LIBSSH2_FREE(session, secret); + ssh2_explicit_zero(secret, session->local.crypt->secret_len); + SSH2_FREE(session, secret); } } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Client to Server IV and Key calculated")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Client to Server IV and Key calculated")); if(session->remote.crypt->dtor) { /* Cleanup any existing cipher */ @@ -460,29 +454,29 @@ static int finish_kex(LIBSSH2_SESSION *session, session->remote.crypt->secret_len, (const unsigned char *)"D"); if(!secret) { - LIBSSH2_FREE(session, iv); + SSH2_FREE(session, iv); return LIBSSH2_ERROR_KEX_FAILURE; } - if(session->remote.crypt-> - init(session, session->remote.crypt, iv, &free_iv, secret, - &free_secret, 0, &session->remote.crypt_abstract)) { - LIBSSH2_FREE(session, iv); - LIBSSH2_FREE(session, secret); + if(session->remote.crypt->init(session, session->remote.crypt, iv, + &free_iv, secret, &free_secret, 0, + &session->remote.crypt_abstract)) { + SSH2_FREE(session, iv); + SSH2_FREE(session, secret); return LIBSSH2_ERROR_KEX_FAILURE; } if(free_iv) { - _libssh2_explicit_zero(iv, session->remote.crypt->iv_len); - LIBSSH2_FREE(session, iv); + ssh2_explicit_zero(iv, session->remote.crypt->iv_len); + SSH2_FREE(session, iv); } if(free_secret) { - _libssh2_explicit_zero(secret, session->remote.crypt->secret_len); - LIBSSH2_FREE(session, secret); + ssh2_explicit_zero(secret, session->remote.crypt->secret_len); + SSH2_FREE(session, secret); } } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Server to Client IV and Key calculated")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Server to Client IV and Key calculated")); if(session->local.mac->dtor) { session->local.mac->dtor(session, &session->local.mac_abstract); @@ -503,12 +497,12 @@ static int finish_kex(LIBSSH2_SESSION *session, &session->local.mac_abstract); if(free_key) { - _libssh2_explicit_zero(key, session->local.mac->key_len); - LIBSSH2_FREE(session, key); + ssh2_explicit_zero(key, session->local.mac->key_len); + SSH2_FREE(session, key); } } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Client to Server HMAC Key calculated")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Client to Server HMAC Key calculated")); if(session->remote.mac->dtor) { session->remote.mac->dtor(session, &session->remote.mac_abstract); @@ -529,12 +523,12 @@ static int finish_kex(LIBSSH2_SESSION *session, &session->remote.mac_abstract); if(free_key) { - _libssh2_explicit_zero(key, session->remote.mac->key_len); - LIBSSH2_FREE(session, key); + ssh2_explicit_zero(key, session->remote.mac->key_len); + SSH2_FREE(session, key); } } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Server to Client HMAC Key calculated")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Server to Client HMAC Key calculated")); /* Initialize compression for each direction */ @@ -549,8 +543,8 @@ static int finish_kex(LIBSSH2_SESSION *session, return LIBSSH2_ERROR_KEX_FAILURE; } } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Client to Server compression initialized")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Client to Server compression initialized")); if(session->remote.comp && session->remote.comp->dtor) { session->remote.comp->dtor(session, 0, &session->remote.comp_abstract); @@ -562,8 +556,8 @@ static int finish_kex(LIBSSH2_SESSION *session, return LIBSSH2_ERROR_KEX_FAILURE; } } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Server to Client compression initialized")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Server to Client compression initialized")); return 0; } @@ -571,51 +565,51 @@ static int finish_kex(LIBSSH2_SESSION *session, static void diffie_hellman_state_cleanup( LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state) { - libssh2_dh_dtor(&exchange_state->x); - _libssh2_bn_free(exchange_state->e); + ssh2_dh_dtor(&exchange_state->x); + ssh2_bn_free(exchange_state->e); exchange_state->e = NULL; - _libssh2_bn_free(exchange_state->f); + ssh2_bn_free(exchange_state->f); exchange_state->f = NULL; - _libssh2_bn_free(exchange_state->k); + ssh2_bn_free(exchange_state->k); exchange_state->k = NULL; - _libssh2_bn_ctx_free(exchange_state->ctx); + ssh2_bn_ctx_free(exchange_state->ctx); exchange_state->ctx = NULL; if(exchange_state->e_packet) { - LIBSSH2_FREE(session, exchange_state->e_packet); + SSH2_FREE(session, exchange_state->e_packet); exchange_state->e_packet = NULL; } if(exchange_state->s_packet) { - LIBSSH2_FREE(session, exchange_state->s_packet); + SSH2_FREE(session, exchange_state->s_packet); exchange_state->s_packet = NULL; } if(exchange_state->k_value) { - LIBSSH2_FREE(session, exchange_state->k_value); + SSH2_FREE(session, exchange_state->k_value); exchange_state->k_value = NULL; } - exchange_state->state = libssh2_NB_state_idle; + exchange_state->state = ssh2_NB_state_idle; } static void kex_diffie_hellman_cleanup( LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state) { - if(key_state->state != libssh2_NB_state_idle) { - _libssh2_bn_free(key_state->p); + if(key_state->state != ssh2_NB_state_idle) { + ssh2_bn_free(key_state->p); key_state->p = NULL; - _libssh2_bn_free(key_state->g); + ssh2_bn_free(key_state->g); key_state->g = NULL; if(key_state->data) { - LIBSSH2_FREE(session, key_state->data); + SSH2_FREE(session, key_state->data); key_state->data = NULL; } - key_state->state = libssh2_NB_state_idle; + key_state->state = ssh2_NB_state_idle; } - if(key_state->exchange_state.state != libssh2_NB_state_idle) { + if(key_state->exchange_state.state != ssh2_NB_state_idle) { diffie_hellman_state_cleanup(session, &key_state->exchange_state); } } @@ -626,8 +620,8 @@ static void kex_diffie_hellman_cleanup( * @result 0 on success, error code on failure */ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, - libssh2_bn *g, - libssh2_bn *p, + ssh2_bn *g, + ssh2_bn *p, int group_order, int sha_algo_value, void *exchange_hash_ctx, @@ -651,22 +645,22 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, else if(sha_algo_value == 1) digest_len = SHA1_DIGEST_LENGTH; else { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "sha algo value is unimplemented"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "SHA algo value is unimplemented"); goto clean_exit; } - if(exchange_state->state == libssh2_NB_state_idle) { + if(exchange_state->state == ssh2_NB_state_idle) { /* Setup initial values */ exchange_state->e_packet = NULL; exchange_state->s_packet = NULL; exchange_state->k_value = NULL; - exchange_state->ctx = _libssh2_bn_ctx_new(); - libssh2_dh_init(&exchange_state->x); - exchange_state->e = _libssh2_bn_init(); /* g^x mod p */ - exchange_state->f = _libssh2_bn_init_from_bin(); /* g^(Random from + exchange_state->ctx = ssh2_bn_ctx_new(); + ssh2_dh_init(&exchange_state->x); + exchange_state->e = ssh2_bn_init(); /* g^x mod p */ + exchange_state->f = ssh2_bn_init_from_bin(); /* g^(Random from server) mod p */ - exchange_state->k = _libssh2_bn_init(); /* The shared secret: f^x mod + exchange_state->k = ssh2_bn_init(); /* The shared secret: f^x mod p */ /* Zero the whole thing out */ @@ -674,88 +668,84 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, sizeof(exchange_state->req_state)); /* Generate x and e */ - if(_libssh2_bn_bits(p) > LIBSSH2_DH_MAX_MODULUS_BITS) { - ret = _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "dh modulus value is too large"); + if(ssh2_bn_bits(p) > SSH2_DH_MAX_MODULUS_BITS) { + ret = ssh2_err(session, LIBSSH2_ERROR_INVAL, + "DH modulus value is too large"); goto clean_exit; } - rc = libssh2_dh_key_pair(&exchange_state->x, exchange_state->e, g, p, - group_order, exchange_state->ctx); + rc = ssh2_dh_key_pair(&exchange_state->x, exchange_state->e, g, p, + group_order, exchange_state->ctx); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "dh key pair generation failed"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "DH key pair generation failed"); goto clean_exit; } /* Send KEX init */ /* packet_type(1) + String Length(4) + leading 0(1) */ - exchange_state->e_packet_len = - _libssh2_bn_bytes(exchange_state->e) + 6; - if(_libssh2_bn_bits(exchange_state->e) % 8) { + exchange_state->e_packet_len = ssh2_bn_bytes(exchange_state->e) + 6; + if(ssh2_bn_bits(exchange_state->e) % 8) { /* Leading 00 not needed */ exchange_state->e_packet_len--; } exchange_state->e_packet = - LIBSSH2_ALLOC(session, exchange_state->e_packet_len); + SSH2_ALLOC(session, exchange_state->e_packet_len); if(!exchange_state->e_packet) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Out of memory error"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Out of memory error"); goto clean_exit; } exchange_state->e_packet[0] = packet_type_init; - _libssh2_htonu32(exchange_state->e_packet + 1, - (uint32_t)(exchange_state->e_packet_len - 5)); - if(_libssh2_bn_bits(exchange_state->e) % 8) { - if(_libssh2_bn_to_bin(exchange_state->e, - exchange_state->e_packet + 5)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->e"); + ssh2_htonu32(exchange_state->e_packet + 1, + (uint32_t)(exchange_state->e_packet_len - 5)); + if(ssh2_bn_bits(exchange_state->e) % 8) { + if(ssh2_bn_to_bin(exchange_state->e, + exchange_state->e_packet + 5)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->e"); goto clean_exit; } } else { exchange_state->e_packet[5] = 0; - if(_libssh2_bn_to_bin(exchange_state->e, - exchange_state->e_packet + 6)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->e"); + if(ssh2_bn_to_bin(exchange_state->e, + exchange_state->e_packet + 6)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->e"); goto clean_exit; } } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, "Sending KEX packet %u", - (unsigned int)packet_type_init)); - exchange_state->state = libssh2_NB_state_created; + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sending KEX packet %u", + (unsigned int)packet_type_init)); + exchange_state->state = ssh2_NB_state_created; } - if(exchange_state->state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, exchange_state->e_packet, - exchange_state->e_packet_len, NULL, 0); + if(exchange_state->state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, exchange_state->e_packet, + exchange_state->e_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send KEX init message"); + ret = ssh2_err(session, rc, "Unable to send KEX init message"); goto clean_exit; } - exchange_state->state = libssh2_NB_state_sent; + exchange_state->state = ssh2_NB_state_sent; } - if(exchange_state->state == libssh2_NB_state_sent) { + if(exchange_state->state == ssh2_NB_state_sent) { if(session->burn_optimistic_kexinit) { /* The first KEX packet to come along is the guess initially * sent by the server. That guess turned out to be wrong so we * need to silently ignore it */ int burn_type; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Waiting for badly guessed KEX packet " - "(to be ignored)")); - burn_type = - _libssh2_packet_burn(session, &exchange_state->burn_state); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Waiting for badly guessed KEX packet (to be ignored)")); + burn_type = ssh2_packet_burn(session, &exchange_state->burn_state); if(burn_type == LIBSSH2_ERROR_EAGAIN) { return burn_type; } @@ -766,30 +756,29 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, } session->burn_optimistic_kexinit = 0; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Burnt packet of type: %02x", - (unsigned int)burn_type)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Burnt packet of type: %02x", + (unsigned int)burn_type)); } - exchange_state->state = libssh2_NB_state_sent1; + exchange_state->state = ssh2_NB_state_sent1; } - if(exchange_state->state == libssh2_NB_state_sent1) { + if(exchange_state->state == ssh2_NB_state_sent1) { /* Wait for KEX reply */ struct string_buf buf; int err; int hok; - rc = _libssh2_packet_require(session, packet_type_reply, - &exchange_state->s_packet, - &exchange_state->s_packet_len, 0, NULL, - 0, &exchange_state->req_state); + rc = ssh2_packet_require(session, packet_type_reply, + &exchange_state->s_packet, + &exchange_state->s_packet_len, 0, NULL, + 0, &exchange_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_TIMEOUT, - "Timed out waiting for KEX reply"); + ret = ssh2_err(session, LIBSSH2_ERROR_TIMEOUT, + "Timed out waiting for KEX reply"); goto clean_exit; } @@ -798,71 +787,71 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, goto clean_exit; } - if(_libssh2_get_string(&buf, &(exchange_state->f_value), - &(exchange_state->f_value_len))) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unable to get DH-SHA f value"); + if(ssh2_get_string(&buf, &(exchange_state->f_value), + &(exchange_state->f_value_len))) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unable to get DH-SHA f value"); goto clean_exit; } - if(_libssh2_bn_from_bin(exchange_state->f, - exchange_state->f_value_len, - exchange_state->f_value)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Invalid DH-SHA f value"); + if(ssh2_bn_from_bin(exchange_state->f, + exchange_state->f_value_len, + exchange_state->f_value)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Invalid DH-SHA f value"); goto clean_exit; } - if(_libssh2_get_string(&buf, &(exchange_state->h_sig), - &(exchange_state->h_sig_len))) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unable to get DH-SHA h sig"); + if(ssh2_get_string(&buf, &(exchange_state->h_sig), + &(exchange_state->h_sig_len))) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unable to get DH-SHA h sig"); goto clean_exit; } /* Compute the shared secret */ - libssh2_dh_secret(&exchange_state->x, exchange_state->k, - exchange_state->f, p, exchange_state->ctx); - exchange_state->k_value_len = _libssh2_bn_bytes(exchange_state->k) + 5; - if(_libssh2_bn_bits(exchange_state->k) % 8) { + ssh2_dh_secret(&exchange_state->x, exchange_state->k, + exchange_state->f, p, exchange_state->ctx); + exchange_state->k_value_len = ssh2_bn_bytes(exchange_state->k) + 5; + if(ssh2_bn_bits(exchange_state->k) % 8) { /* do not need leading 00 */ exchange_state->k_value_len--; } exchange_state->k_value = - LIBSSH2_ALLOC(session, exchange_state->k_value_len); + SSH2_ALLOC(session, exchange_state->k_value_len); if(!exchange_state->k_value) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate buffer for DH-SHA K"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate buffer for DH-SHA K"); goto clean_exit; } - _libssh2_htonu32(exchange_state->k_value, - (uint32_t)(exchange_state->k_value_len - 4)); - if(_libssh2_bn_bits(exchange_state->k) % 8) { - if(_libssh2_bn_to_bin(exchange_state->k, - exchange_state->k_value + 4)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->k"); + ssh2_htonu32(exchange_state->k_value, + (uint32_t)(exchange_state->k_value_len - 4)); + if(ssh2_bn_bits(exchange_state->k) % 8) { + if(ssh2_bn_to_bin(exchange_state->k, + exchange_state->k_value + 4)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->k"); goto clean_exit; } } else { exchange_state->k_value[4] = 0; - if(_libssh2_bn_to_bin(exchange_state->k, - exchange_state->k_value + 5)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->k"); + if(ssh2_bn_to_bin(exchange_state->k, + exchange_state->k_value + 5)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->k"); goto clean_exit; } } if(!sha_algo_ctx_init(sha_algo_value, exchange_hash_ctx)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_INIT, - "Unable to initialize hash context"); + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_INIT, + "Unable to initialize hash context"); goto clean_exit; } hok = 1; if(session->local.banner) { - _libssh2_htonu32(exchange_state->h_sig_comp, + ssh2_htonu32(exchange_state->h_sig_comp, (uint32_t)(strlen((char *)session->local.banner) - 2)); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 4); @@ -871,8 +860,8 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, strlen((char *)session->local.banner) - 2); } else { - _libssh2_htonu32(exchange_state->h_sig_comp, - sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); + ssh2_htonu32(exchange_state->h_sig_comp, + sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 4); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, @@ -880,32 +869,31 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); } - _libssh2_htonu32(exchange_state->h_sig_comp, - (uint32_t)strlen((char *)session->remote.banner)); + ssh2_htonu32(exchange_state->h_sig_comp, + (uint32_t)strlen((char *)session->remote.banner)); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 4); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, session->remote.banner, strlen((char *)session->remote.banner)); - _libssh2_htonu32(exchange_state->h_sig_comp, - (uint32_t)session->local.kexinit_len); + ssh2_htonu32(exchange_state->h_sig_comp, + (uint32_t)session->local.kexinit_len); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 4); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, session->local.kexinit, session->local.kexinit_len); - _libssh2_htonu32(exchange_state->h_sig_comp, - (uint32_t)session->remote.kexinit_len); + ssh2_htonu32(exchange_state->h_sig_comp, + (uint32_t)session->remote.kexinit_len); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 4); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, session->remote.kexinit, session->remote.kexinit_len); - _libssh2_htonu32(exchange_state->h_sig_comp, - session->server_hostkey_len); + ssh2_htonu32(exchange_state->h_sig_comp, session->server_hostkey_len); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 4); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, @@ -914,12 +902,9 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, if(packet_type_init == SSH_MSG_KEX_DH_GEX_INIT) { /* diffie-hellman-group-exchange hashes additional fields */ - _libssh2_htonu32(exchange_state->h_sig_comp, - LIBSSH2_DH_GEX_MINGROUP); - _libssh2_htonu32(exchange_state->h_sig_comp + 4, - LIBSSH2_DH_GEX_OPTGROUP); - _libssh2_htonu32(exchange_state->h_sig_comp + 8, - LIBSSH2_DH_GEX_MAXGROUP); + ssh2_htonu32(exchange_state->h_sig_comp, SSH2_DH_GEX_MINGROUP); + ssh2_htonu32(exchange_state->h_sig_comp + 4, SSH2_DH_GEX_OPTGROUP); + ssh2_htonu32(exchange_state->h_sig_comp + 8, SSH2_DH_GEX_MAXGROUP); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 12); } @@ -933,8 +918,8 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, exchange_state->e_packet + 1, exchange_state->e_packet_len - 1); - _libssh2_htonu32(exchange_state->h_sig_comp, - (uint32_t)exchange_state->f_value_len); + ssh2_htonu32(exchange_state->h_sig_comp, + (uint32_t)exchange_state->f_value_len); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, exchange_state->h_sig_comp, 4); hok &= sha_algo_ctx_update(sha_algo_value, exchange_hash_ctx, @@ -945,11 +930,10 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, exchange_state->k_value, exchange_state->k_value_len); - if(!hok || - !sha_algo_ctx_final(sha_algo_value, exchange_hash_ctx, - exchange_state->h_sig_comp)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_CALC, - "kex: failed to calculate hash"); + if(!hok || !sha_algo_ctx_final(sha_algo_value, exchange_hash_ctx, + exchange_state->h_sig_comp)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC, + "kex: failed to calculate hash"); goto clean_exit; } @@ -961,37 +945,35 @@ static int diffie_hellman_sha_algo(LIBSSH2_SESSION *session, &session->server_hostkey_abstract); if(err) { - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Failed hostkey sig_verify(): %s: %d", - session->hostkey->name, err)); - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN, - "Unable to verify hostkey signature " - "DH-SHA"); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Failed hostkey sig_verify(): %s: %d", + session->hostkey->name, err)); + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_SIGN, + "Unable to verify hostkey signature DH-SHA"); goto clean_exit; } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sending NEWKEYS message")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sending NEWKEYS message")); exchange_state->c = SSH_MSG_NEWKEYS; - exchange_state->state = libssh2_NB_state_sent2; + exchange_state->state = ssh2_NB_state_sent2; } - if(exchange_state->state == libssh2_NB_state_sent2) { - rc = _libssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); + if(exchange_state->state == ssh2_NB_state_sent2) { + rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send NEWKEYS message DH-SHA"); + ret = ssh2_err(session, rc, + "Unable to send NEWKEYS message DH-SHA"); goto clean_exit; } - exchange_state->state = libssh2_NB_state_sent3; + exchange_state->state = ssh2_NB_state_sent3; } - if(exchange_state->state == libssh2_NB_state_sent3) { + if(exchange_state->state == ssh2_NB_state_sent3) { ret = finish_kex(session, exchange_state, digest_len, sha_algo_value); if(ret == LIBSSH2_ERROR_EAGAIN) { return ret; @@ -1030,30 +1012,30 @@ static int kex_method_diffie_hellman_group1_sha1_key_exchange( }; int ret; - libssh2_sha1_ctx exchange_hash_ctx; + ssh2_sha1_ctx exchange_hash_ctx; - if(key_state->state == libssh2_NB_state_idle) { + if(key_state->state == ssh2_NB_state_idle) { /* g == 2 */ - key_state->p = _libssh2_bn_init_from_bin(); /* SSH2 defined value + key_state->p = ssh2_bn_init_from_bin(); /* SSH2 defined value (p_value) */ - key_state->g = _libssh2_bn_init(); /* SSH2 defined value (2) */ + key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */ /* Initialize P and G */ - if(!key_state->g || _libssh2_bn_set_word(key_state->g, 2)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state g."); + if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state g."); goto clean_exit; } - if(!key_state->p || _libssh2_bn_from_bin(key_state->p, 128, p_value)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state p."); + if(!key_state->p || ssh2_bn_from_bin(key_state->p, 128, p_value)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state p."); goto clean_exit; } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating Diffie-Hellman Group1 Key Exchange")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating Diffie-Hellman Group1 Key Exchange")); - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } ret = diffie_hellman_sha_algo(session, key_state->g, key_state->p, 128, 1, @@ -1075,8 +1057,8 @@ clean_exit: */ typedef int (*diffie_hellman_hash_func_t)( LIBSSH2_SESSION *session, - libssh2_bn *g, - libssh2_bn *p, + ssh2_bn *g, + ssh2_bn *p, int group_order, int sha_algo_value, void *exchange_hash_ctx, @@ -1128,29 +1110,29 @@ static int kex_method_diffie_hellman_group14_key_exchange( }; int ret; - if(key_state->state == libssh2_NB_state_idle) { - key_state->p = _libssh2_bn_init_from_bin(); /* SSH2 defined value + if(key_state->state == ssh2_NB_state_idle) { + key_state->p = ssh2_bn_init_from_bin(); /* SSH2 defined value (p_value) */ - key_state->g = _libssh2_bn_init(); /* SSH2 defined value (2) */ + key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */ /* g == 2 */ /* Initialize P and G */ - if(!key_state->g || _libssh2_bn_set_word(key_state->g, 2)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state g."); + if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state g."); goto clean_exit; } else if(!key_state->p || - _libssh2_bn_from_bin(key_state->p, 256, p_value)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state p."); + ssh2_bn_from_bin(key_state->p, 256, p_value)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state p."); goto clean_exit; } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating Diffie-Hellman Group14 Key Exchange")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating Diffie-Hellman Group14 Key Exchange")); - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } ret = hashfunc(session, key_state->g, key_state->p, 256, sha_algo_value, exchange_hash_ctx, SSH_MSG_KEXDH_INIT, @@ -1171,11 +1153,9 @@ clean_exit: static int kex_method_diffie_hellman_group14_sha1_key_exchange( LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state) { - libssh2_sha1_ctx ctx; - return kex_method_diffie_hellman_group14_key_exchange(session, - key_state, 1, - &ctx, - diffie_hellman_sha_algo); + ssh2_sha1_ctx ctx; + return kex_method_diffie_hellman_group14_key_exchange( + session, key_state, 1, &ctx, diffie_hellman_sha_algo); } /* @@ -1184,11 +1164,9 @@ static int kex_method_diffie_hellman_group14_sha1_key_exchange( static int kex_method_diffie_hellman_group14_sha256_key_exchange( LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state) { - libssh2_sha256_ctx ctx; - return kex_method_diffie_hellman_group14_key_exchange(session, - key_state, 256, - &ctx, - diffie_hellman_sha_algo); + ssh2_sha256_ctx ctx; + return kex_method_diffie_hellman_group14_key_exchange( + session, key_state, 256, &ctx, diffie_hellman_sha_algo); } /* @@ -1243,30 +1221,30 @@ static int kex_method_diffie_hellman_group16_sha512_key_exchange( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; int ret; - libssh2_sha512_ctx exchange_hash_ctx; + ssh2_sha512_ctx exchange_hash_ctx; - if(key_state->state == libssh2_NB_state_idle) { - key_state->p = _libssh2_bn_init_from_bin(); /* SSH2 defined value + if(key_state->state == ssh2_NB_state_idle) { + key_state->p = ssh2_bn_init_from_bin(); /* SSH2 defined value (p_value) */ - key_state->g = _libssh2_bn_init(); /* SSH2 defined value (2) */ + key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */ /* g == 2 */ /* Initialize P and G */ - if(!key_state->g || _libssh2_bn_set_word(key_state->g, 2)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state g."); + if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state g."); goto clean_exit; } - if(!key_state->p || _libssh2_bn_from_bin(key_state->p, 512, p_value)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state p."); + if(!key_state->p || ssh2_bn_from_bin(key_state->p, 512, p_value)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state p."); goto clean_exit; } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating Diffie-Hellman Group16 Key Exchange")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating Diffie-Hellman Group16 Key Exchange")); - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } ret = diffie_hellman_sha_algo(session, key_state->g, key_state->p, 512, @@ -1378,31 +1356,31 @@ static int kex_method_diffie_hellman_group18_sha512_key_exchange( 0xFF, 0xFF, 0xFF, 0xFF }; int ret; - libssh2_sha512_ctx exchange_hash_ctx; + ssh2_sha512_ctx exchange_hash_ctx; - if(key_state->state == libssh2_NB_state_idle) { - key_state->p = _libssh2_bn_init_from_bin(); /* SSH2 defined value + if(key_state->state == ssh2_NB_state_idle) { + key_state->p = ssh2_bn_init_from_bin(); /* SSH2 defined value (p_value) */ - key_state->g = _libssh2_bn_init(); /* SSH2 defined value (2) */ + key_state->g = ssh2_bn_init(); /* SSH2 defined value (2) */ /* g == 2 */ /* Initialize P and G */ - if(!key_state->g || _libssh2_bn_set_word(key_state->g, 2)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state g."); + if(!key_state->g || ssh2_bn_set_word(key_state->g, 2)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state g."); goto clean_exit; } else if(!key_state->p || - _libssh2_bn_from_bin(key_state->p, 1024, p_value)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to allocate key state p."); + ssh2_bn_from_bin(key_state->p, 1024, p_value)) { + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to allocate key state p."); goto clean_exit; } - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating Diffie-Hellman Group18 Key Exchange")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating Diffie-Hellman Group18 Key Exchange")); - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } ret = diffie_hellman_sha_algo(session, key_state->g, key_state->p, 1024, @@ -1429,62 +1407,61 @@ static int kex_method_diffie_hellman_group_exchange_sha1_key_exchange( int ret = 0; int rc; - if(key_state->state == libssh2_NB_state_idle) { - key_state->p = _libssh2_bn_init_from_bin(); - key_state->g = _libssh2_bn_init_from_bin(); + if(key_state->state == ssh2_NB_state_idle) { + key_state->p = ssh2_bn_init_from_bin(); + key_state->g = ssh2_bn_init_from_bin(); /* Ask for a P and G pair */ key_state->request[0] = SSH_MSG_KEX_DH_GEX_REQUEST; - _libssh2_htonu32(key_state->request + 1, LIBSSH2_DH_GEX_MINGROUP); - _libssh2_htonu32(key_state->request + 5, LIBSSH2_DH_GEX_OPTGROUP); - _libssh2_htonu32(key_state->request + 9, LIBSSH2_DH_GEX_MAXGROUP); + ssh2_htonu32(key_state->request + 1, SSH2_DH_GEX_MINGROUP); + ssh2_htonu32(key_state->request + 5, SSH2_DH_GEX_OPTGROUP); + ssh2_htonu32(key_state->request + 9, SSH2_DH_GEX_MAXGROUP); key_state->request_len = 13; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating Diffie-Hellman Group-Exchange SHA1")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating Diffie-Hellman Group-Exchange SHA1")); - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } - if(key_state->state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, key_state->request, - key_state->request_len, NULL, 0); + if(key_state->state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, key_state->request, + key_state->request_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send Group Exchange Request"); + ret = ssh2_err(session, rc, + "Unable to send Group Exchange Request"); goto dh_gex_clean_exit; } - key_state->state = libssh2_NB_state_sent; + key_state->state = ssh2_NB_state_sent; } - if(key_state->state == libssh2_NB_state_sent) { - rc = _libssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP, - &key_state->data, &key_state->data_len, - 0, NULL, 0, &key_state->req_state); + if(key_state->state == ssh2_NB_state_sent) { + rc = ssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP, + &key_state->data, &key_state->data_len, + 0, NULL, 0, &key_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Timeout waiting for GEX_GROUP reply"); + ret = ssh2_err(session, rc, "Timeout waiting for GEX_GROUP reply"); goto dh_gex_clean_exit; } - key_state->state = libssh2_NB_state_sent1; + key_state->state = ssh2_NB_state_sent1; } - if(key_state->state == libssh2_NB_state_sent1) { + if(key_state->state == ssh2_NB_state_sent1) { size_t p_len, g_len; unsigned char *p, *g; struct string_buf buf; - libssh2_sha1_ctx exchange_hash_ctx; + ssh2_sha1_ctx exchange_hash_ctx; int bits; if(key_state->data_len < 9) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected key length DH-SHA1"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected key length DH-SHA1"); goto dh_gex_clean_exit; } @@ -1494,35 +1471,33 @@ static int kex_method_diffie_hellman_group_exchange_sha1_key_exchange( buf.dataptr++; /* increment to big num */ - if(_libssh2_get_bignum_bytes(&buf, &p, &p_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected value DH-SHA1 p"); + if(ssh2_get_bignum_bytes(&buf, &p, &p_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected value DH-SHA1 p"); goto dh_gex_clean_exit; } - if(_libssh2_get_bignum_bytes(&buf, &g, &g_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected value DH-SHA1 g"); + if(ssh2_get_bignum_bytes(&buf, &g, &g_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected value DH-SHA1 g"); goto dh_gex_clean_exit; } - if(_libssh2_bn_from_bin(key_state->p, p_len, p)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid DH-SHA1 p"); + if(ssh2_bn_from_bin(key_state->p, p_len, p)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "Invalid DH-SHA1 p"); goto dh_gex_clean_exit; } - bits = (int)_libssh2_bn_bits(key_state->p); - if(bits < LIBSSH2_DH_GEX_MINGROUP || - bits > LIBSSH2_DH_GEX_MAXGROUP) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "DH-SHA1 p out of range"); + bits = (int)ssh2_bn_bits(key_state->p); + if(bits < SSH2_DH_GEX_MINGROUP || + bits > SSH2_DH_GEX_MAXGROUP) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "DH-SHA1 p out of range"); goto dh_gex_clean_exit; } - if(_libssh2_bn_from_bin(key_state->g, g_len, g)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid DH-SHA1 g"); + if(ssh2_bn_from_bin(key_state->g, g_len, g)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "Invalid DH-SHA1 g"); goto dh_gex_clean_exit; } @@ -1555,63 +1530,62 @@ static int kex_method_diffie_hellman_group_exchange_sha256_key_exchange( int ret = 0; int rc; - if(key_state->state == libssh2_NB_state_idle) { - key_state->p = _libssh2_bn_init_from_bin(); - key_state->g = _libssh2_bn_init_from_bin(); + if(key_state->state == ssh2_NB_state_idle) { + key_state->p = ssh2_bn_init_from_bin(); + key_state->g = ssh2_bn_init_from_bin(); /* Ask for a P and G pair */ key_state->request[0] = SSH_MSG_KEX_DH_GEX_REQUEST; - _libssh2_htonu32(key_state->request + 1, LIBSSH2_DH_GEX_MINGROUP); - _libssh2_htonu32(key_state->request + 5, LIBSSH2_DH_GEX_OPTGROUP); - _libssh2_htonu32(key_state->request + 9, LIBSSH2_DH_GEX_MAXGROUP); + ssh2_htonu32(key_state->request + 1, SSH2_DH_GEX_MINGROUP); + ssh2_htonu32(key_state->request + 5, SSH2_DH_GEX_OPTGROUP); + ssh2_htonu32(key_state->request + 9, SSH2_DH_GEX_MAXGROUP); key_state->request_len = 13; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating Diffie-Hellman Group-Exchange SHA256")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating Diffie-Hellman Group-Exchange SHA256")); - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } - if(key_state->state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, key_state->request, - key_state->request_len, NULL, 0); + if(key_state->state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, key_state->request, + key_state->request_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send " - "Group Exchange Request SHA256"); + ret = ssh2_err(session, rc, + "Unable to send Group Exchange Request SHA256"); goto dh_gex_clean_exit; } - key_state->state = libssh2_NB_state_sent; + key_state->state = ssh2_NB_state_sent; } - if(key_state->state == libssh2_NB_state_sent) { - rc = _libssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP, - &key_state->data, &key_state->data_len, - 0, NULL, 0, &key_state->req_state); + if(key_state->state == ssh2_NB_state_sent) { + rc = ssh2_packet_require(session, SSH_MSG_KEX_DH_GEX_GROUP, + &key_state->data, &key_state->data_len, + 0, NULL, 0, &key_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Timeout waiting for GEX_GROUP reply SHA256"); + ret = ssh2_err(session, rc, + "Timeout waiting for GEX_GROUP reply SHA256"); goto dh_gex_clean_exit; } - key_state->state = libssh2_NB_state_sent1; + key_state->state = ssh2_NB_state_sent1; } - if(key_state->state == libssh2_NB_state_sent1) { + if(key_state->state == ssh2_NB_state_sent1) { unsigned char *p, *g; size_t p_len, g_len; struct string_buf buf; - libssh2_sha256_ctx exchange_hash_ctx; + ssh2_sha256_ctx exchange_hash_ctx; int bits; if(key_state->data_len < 9) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected key length DH-SHA256"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected key length DH-SHA256"); goto dh_gex_clean_exit; } @@ -1621,35 +1595,35 @@ static int kex_method_diffie_hellman_group_exchange_sha256_key_exchange( buf.dataptr++; /* increment to big num */ - if(_libssh2_get_bignum_bytes(&buf, &p, &p_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected value DH-SHA256 p"); + if(ssh2_get_bignum_bytes(&buf, &p, &p_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected value DH-SHA256 p"); goto dh_gex_clean_exit; } - if(_libssh2_get_bignum_bytes(&buf, &g, &g_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected value DH-SHA256 g"); + if(ssh2_get_bignum_bytes(&buf, &g, &g_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected value DH-SHA256 g"); goto dh_gex_clean_exit; } - if(_libssh2_bn_from_bin(key_state->p, p_len, p)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid DH-SHA256 p"); + if(ssh2_bn_from_bin(key_state->p, p_len, p)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Invalid DH-SHA256 p"); goto dh_gex_clean_exit; } - bits = (int)_libssh2_bn_bits(key_state->p); - if(bits < LIBSSH2_DH_GEX_MINGROUP || - bits > LIBSSH2_DH_GEX_MAXGROUP) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "DH-SHA256 p out of range"); + bits = (int)ssh2_bn_bits(key_state->p); + if(bits < SSH2_DH_GEX_MINGROUP || + bits > SSH2_DH_GEX_MAXGROUP) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "DH-SHA256 p out of range"); goto dh_gex_clean_exit; } - if(_libssh2_bn_from_bin(key_state->g, g_len, g)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid DH-SHA256 g"); + if(ssh2_bn_from_bin(key_state->g, g_len, g)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Invalid DH-SHA256 g"); goto dh_gex_clean_exit; } @@ -1678,17 +1652,17 @@ dh_gex_clean_exit: * returns the EC curve type by name used in hybrid key exchange */ static int kex_session_hybrid_curve_type(const char *name, - libssh2_curve_type *out_type) + ssh2_curve_type *out_type) { - libssh2_curve_type type; + ssh2_curve_type type; if(!name) return -1; if(strcmp(name, "mlkem768nistp256-sha256") == 0) - type = LIBSSH2_EC_CURVE_NISTP256; + type = SSH2_EC_CURVE_NISTP256; else if(strcmp(name, "mlkem1024nistp384-sha384") == 0) - type = LIBSSH2_EC_CURVE_NISTP384; + type = SSH2_EC_CURVE_NISTP384; else { return -1; } @@ -1718,89 +1692,88 @@ static int kex_session_hybrid_curve_type(const char *name, * string Q_S, server's ephemeral public key octet string * mpint K, shared secret */ -#define LIBSSH2_KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(digest_type) \ +#define KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(digest_type) \ do { \ - libssh2_sha##digest_type##_ctx ctx; \ + ssh2_sha##digest_type##_ctx ctx; \ int hok; \ - if(!libssh2_sha##digest_type##_init(&ctx)) { \ + if(!ssh2_sha##digest_type##_init(&ctx)) { \ rc = -1; \ break; \ } \ hok = 1; \ if(session->local.banner) { \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)(strlen((char *)session->local.banner) - 2)); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)(strlen((char *)session->local.banner) - 2)); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + hok &= ssh2_sha##digest_type##_update(ctx, \ (char *)session->local.banner, \ strlen((char *)session->local.banner) - 2); \ } \ else { \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + hok &= ssh2_sha##digest_type##_update(ctx, \ LIBSSH2_SSH_DEFAULT_BANNER, \ sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); \ } \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)strlen((char *)session->remote.banner)); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)strlen((char *)session->remote.banner)); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + hok &= ssh2_sha##digest_type##_update(ctx, \ session->remote.banner, \ strlen((char *)session->remote.banner)); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)session->local.kexinit_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)session->local.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - session->local.kexinit, \ - session->local.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + session->local.kexinit, \ + session->local.kexinit_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)session->remote.kexinit_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)session->remote.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - session->remote.kexinit, \ - session->remote.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + session->remote.kexinit, \ + session->remote.kexinit_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - session->server_hostkey_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + session->server_hostkey_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - session->server_hostkey, \ - session->server_hostkey_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + session->server_hostkey, \ + session->server_hostkey_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)public_key_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)public_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - public_key, \ - public_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + public_key, \ + public_key_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)server_public_key_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)server_public_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - server_public_key, \ - server_public_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + server_public_key, \ + server_public_key_len); \ \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - exchange_state->k_value, \ - exchange_state->k_value_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + exchange_state->k_value, \ + exchange_state->k_value_len); \ \ if(!hok || \ - !libssh2_sha##digest_type##_final(ctx, \ - exchange_state->h_sig_comp)) { \ + !ssh2_sha##digest_type##_final(ctx, exchange_state->h_sig_comp)) { \ rc = -1; \ break; \ } \ @@ -1831,92 +1804,91 @@ static int kex_session_hybrid_curve_type(const char *name, * string Q_S, server's ephemeral public key octet string * mpint K, shared secret */ -#define LIBSSH2_KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(digest_type) \ +#define KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(digest_type) \ do { \ - libssh2_sha##digest_type##_ctx ctx; \ + ssh2_sha##digest_type##_ctx ctx; \ int hok; \ - if(!libssh2_sha##digest_type##_init(&ctx)) { \ + if(!ssh2_sha##digest_type##_init(&ctx)) { \ rc = -1; \ break; \ } \ hok = 1; \ if(session->local.banner) { \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)(strlen((char *)session->local.banner) - 2)); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)(strlen((char *)session->local.banner) - 2)); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + hok &= ssh2_sha##digest_type##_update(ctx, \ (char *)session->local.banner, \ strlen((char *)session->local.banner) - 2); \ } \ else { \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + hok &= ssh2_sha##digest_type##_update(ctx, \ LIBSSH2_SSH_DEFAULT_BANNER, \ sizeof(LIBSSH2_SSH_DEFAULT_BANNER) - 1); \ } \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)strlen((char *)session->remote.banner)); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)strlen((char *)session->remote.banner)); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + hok &= ssh2_sha##digest_type##_update(ctx, \ session->remote.banner, \ strlen((char *)session->remote.banner)); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)session->local.kexinit_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)session->local.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - session->local.kexinit, \ - session->local.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + session->local.kexinit, \ + session->local.kexinit_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)session->remote.kexinit_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)session->remote.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - session->remote.kexinit, \ - session->remote.kexinit_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + session->remote.kexinit, \ + session->remote.kexinit_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - session->server_hostkey_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + session->server_hostkey_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - session->server_hostkey, \ - session->server_hostkey_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + session->server_hostkey, \ + session->server_hostkey_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)(public_pq_key_len + public_t_key_len)); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)(public_pq_key_len + public_t_key_len)); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - public_pq_key, \ - public_pq_key_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - public_t_key, \ - public_t_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + public_pq_key, \ + public_pq_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + public_t_key, \ + public_t_key_len); \ \ - _libssh2_htonu32(exchange_state->h_sig_comp, \ - (uint32_t)server_public_key_len); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ + ssh2_htonu32(exchange_state->h_sig_comp, \ + (uint32_t)server_public_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ exchange_state->h_sig_comp, 4); \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - server_public_key, \ - server_public_key_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + server_public_key, \ + server_public_key_len); \ \ - hok &= libssh2_sha##digest_type##_update(ctx, \ - exchange_state->k_value, \ - exchange_state->k_value_len); \ + hok &= ssh2_sha##digest_type##_update(ctx, \ + exchange_state->k_value, \ + exchange_state->k_value_len); \ \ if(!hok || \ - !libssh2_sha##digest_type##_final(ctx, \ - exchange_state->h_sig_comp)) { \ + !ssh2_sha##digest_type##_final(ctx, exchange_state->h_sig_comp)) { \ rc = -1; \ break; \ } \ @@ -1936,19 +1908,19 @@ static int kex_session_hybrid_curve_type(const char *name, * returns the EC curve type by name used in key exchange */ static int kex_session_ecdh_curve_type(const char *name, - libssh2_curve_type *out_type) + ssh2_curve_type *out_type) { - libssh2_curve_type type; + ssh2_curve_type type; if(!name) return -1; if(strcmp(name, "ecdh-sha2-nistp256") == 0) - type = LIBSSH2_EC_CURVE_NISTP256; + type = SSH2_EC_CURVE_NISTP256; else if(strcmp(name, "ecdh-sha2-nistp384") == 0) - type = LIBSSH2_EC_CURVE_NISTP384; + type = SSH2_EC_CURVE_NISTP384; else if(strcmp(name, "ecdh-sha2-nistp521") == 0) - type = LIBSSH2_EC_CURVE_NISTP521; + type = SSH2_EC_CURVE_NISTP521; else { return -1; } @@ -1963,38 +1935,38 @@ static int kex_session_ecdh_curve_type(const char *name, static void ecdh_exchange_state_cleanup( LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state) { - _libssh2_bn_free(exchange_state->k); + ssh2_bn_free(exchange_state->k); exchange_state->k = NULL; if(exchange_state->k_value) { - LIBSSH2_FREE(session, exchange_state->k_value); + SSH2_FREE(session, exchange_state->k_value); exchange_state->k_value = NULL; } - exchange_state->state = libssh2_NB_state_idle; + exchange_state->state = ssh2_NB_state_idle; } static void kex_method_ecdh_cleanup(LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state) { if(key_state->public_key_oct) { - LIBSSH2_FREE(session, key_state->public_key_oct); + SSH2_FREE(session, key_state->public_key_oct); key_state->public_key_oct = NULL; } if(key_state->private_key) { - _libssh2_ecdsa_free(key_state->private_key); + ssh2_ecdsa_free(key_state->private_key); key_state->private_key = NULL; } if(key_state->data) { - LIBSSH2_FREE(session, key_state->data); + SSH2_FREE(session, key_state->data); key_state->data = NULL; } - key_state->state = libssh2_NB_state_idle; + key_state->state = ssh2_NB_state_idle; - if(key_state->exchange_state.state != libssh2_NB_state_idle) { + if(key_state->exchange_state.state != ssh2_NB_state_idle) { ecdh_exchange_state_cleanup(session, &key_state->exchange_state); } } @@ -2002,24 +1974,24 @@ static void kex_method_ecdh_cleanup(LIBSSH2_SESSION *session, /* * Elliptic Curve Diffie Hellman Key Exchange */ -static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type, +static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, ssh2_curve_type type, unsigned char *data, size_t data_len, unsigned char *public_key, - size_t public_key_len, libssh2_ec_key *private_key, + size_t public_key_len, ssh2_ec_key *private_key, struct kmdhgGPshakex_state *exchange_state) { int ret = 0; int rc; - if(exchange_state->state == libssh2_NB_state_idle) { + if(exchange_state->state == ssh2_NB_state_idle) { /* Setup initial values */ - exchange_state->k = _libssh2_bn_init(); + exchange_state->k = ssh2_bn_init(); - exchange_state->state = libssh2_NB_state_created; + exchange_state->state = ssh2_NB_state_created; } - if(exchange_state->state == libssh2_NB_state_created) { + if(exchange_state->state == ssh2_NB_state_created) { /* parse INIT reply data */ /* host key K_S */ @@ -2028,8 +2000,8 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type, struct string_buf buf; if(!data) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Missing host key data"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Missing host key data"); goto clean_exit; } @@ -2039,58 +2011,57 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type, } /* server public key Q_S */ - if(_libssh2_get_string(&buf, &server_public_key, - &server_public_key_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected key length ECDH"); + if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected key length ECDH"); goto clean_exit; } /* server signature */ - if(_libssh2_get_string(&buf, &exchange_state->h_sig, - &(exchange_state->h_sig_len))) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unexpected ECDH server sig length"); + if(ssh2_get_string(&buf, &exchange_state->h_sig, + &(exchange_state->h_sig_len))) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unexpected ECDH server sig length"); goto clean_exit; } /* Compute the shared secret K */ - rc = _libssh2_ecdh_gen_k(&exchange_state->k, private_key, - server_public_key, server_public_key_len); + rc = ssh2_ecdh_gen_k(&exchange_state->k, private_key, + server_public_key, server_public_key_len); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "Unable to create ECDH shared secret"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "Unable to create ECDH shared secret"); goto clean_exit; } - exchange_state->k_value_len = _libssh2_bn_bytes(exchange_state->k) + 5; - if(_libssh2_bn_bits(exchange_state->k) % 8) { + exchange_state->k_value_len = ssh2_bn_bytes(exchange_state->k) + 5; + if(ssh2_bn_bits(exchange_state->k) % 8) { /* do not need leading 00 */ exchange_state->k_value_len--; } exchange_state->k_value = - LIBSSH2_ALLOC(session, exchange_state->k_value_len); + SSH2_ALLOC(session, exchange_state->k_value_len); if(!exchange_state->k_value) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate buffer for ECDH K"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate buffer for ECDH K"); goto clean_exit; } - _libssh2_htonu32(exchange_state->k_value, - (uint32_t)(exchange_state->k_value_len - 4)); - if(_libssh2_bn_bits(exchange_state->k) % 8) { - if(_libssh2_bn_to_bin(exchange_state->k, - exchange_state->k_value + 4)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->k"); + ssh2_htonu32(exchange_state->k_value, + (uint32_t)(exchange_state->k_value_len - 4)); + if(ssh2_bn_bits(exchange_state->k) % 8) { + if(ssh2_bn_to_bin(exchange_state->k, + exchange_state->k_value + 4)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->k"); goto clean_exit; } } else { exchange_state->k_value[4] = 0; - if(_libssh2_bn_to_bin(exchange_state->k, - exchange_state->k_value + 5)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->k"); + if(ssh2_bn_to_bin(exchange_state->k, + exchange_state->k_value + 5)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->k"); goto clean_exit; } } @@ -2098,61 +2069,59 @@ static int ecdh_sha2_nistp(LIBSSH2_SESSION *session, libssh2_curve_type type, /* verify hash */ switch(type) { - case LIBSSH2_EC_CURVE_NISTP256: - LIBSSH2_KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(256); + case SSH2_EC_CURVE_NISTP256: + KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(256); break; - case LIBSSH2_EC_CURVE_NISTP384: - LIBSSH2_KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(384); + case SSH2_EC_CURVE_NISTP384: + KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(384); break; - case LIBSSH2_EC_CURVE_NISTP521: - LIBSSH2_KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(512); + case SSH2_EC_CURVE_NISTP521: + KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(512); break; } if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN, - "Unable to verify hostkey signature " - "ECDH"); + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_SIGN, + "Unable to verify hostkey signature ECDH"); goto clean_exit; } exchange_state->c = SSH_MSG_NEWKEYS; - exchange_state->state = libssh2_NB_state_sent; + exchange_state->state = ssh2_NB_state_sent; } - if(exchange_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); + if(exchange_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send NEWKEYS message ECDH"); + ret = ssh2_err(session, rc, "Unable to send NEWKEYS message ECDH"); goto clean_exit; } - exchange_state->state = libssh2_NB_state_sent2; + exchange_state->state = ssh2_NB_state_sent2; } - if(exchange_state->state == libssh2_NB_state_sent2) { + if(exchange_state->state == ssh2_NB_state_sent2) { int digest_len = 0; int sha_algo_value = 0; - if(type == LIBSSH2_EC_CURVE_NISTP256) { + if(type == SSH2_EC_CURVE_NISTP256) { digest_len = SHA256_DIGEST_LENGTH; sha_algo_value = 256; } - else if(type == LIBSSH2_EC_CURVE_NISTP384) { + else if(type == SSH2_EC_CURVE_NISTP384) { digest_len = SHA384_DIGEST_LENGTH; sha_algo_value = 384; } - else if(type == LIBSSH2_EC_CURVE_NISTP521) { + else if(type == SSH2_EC_CURVE_NISTP521) { digest_len = SHA512_DIGEST_LENGTH; sha_algo_value = 512; } else { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "Unknown SHA digest for EC curve"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "Unknown SHA digest for EC curve"); goto clean_exit; } ret = finish_kex(session, exchange_state, digest_len, sha_algo_value); @@ -2177,79 +2146,79 @@ static int kex_method_ecdh_key_exchange( int ret = 0; int rc = 0; unsigned char *s; - libssh2_curve_type type; + ssh2_curve_type type; - if(key_state->state == libssh2_NB_state_idle) { + if(key_state->state == ssh2_NB_state_idle) { key_state->public_key_oct = NULL; - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } - if(key_state->state == libssh2_NB_state_created) { + if(key_state->state == ssh2_NB_state_created) { rc = kex_session_ecdh_curve_type(session->kex->name, &type); if(rc) { - ret = _libssh2_error(session, -1, "Unknown KEX nistp curve type"); + ret = ssh2_err(session, -1, "Unknown KEX nistp curve type"); goto ecdh_clean_exit; } - rc = _libssh2_ecdsa_create_key(session, &key_state->private_key, - &key_state->public_key_oct, - &key_state->public_key_oct_len, type); + rc = ssh2_ecdsa_create_key(session, &key_state->private_key, + &key_state->public_key_oct, + &key_state->public_key_oct_len, type); if(rc) { - ret = _libssh2_error(session, rc, "Unable to create private key"); + ret = ssh2_err(session, rc, "Unable to create private key"); goto ecdh_clean_exit; } key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT; s = key_state->request + 1; - _libssh2_store_str(&s, (const char *)key_state->public_key_oct, - key_state->public_key_oct_len); + ssh2_store_str(&s, (const char *)key_state->public_key_oct, + key_state->public_key_oct_len); key_state->request_len = key_state->public_key_oct_len + 5; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating ECDH SHA2 NISTP256")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating ECDH SHA2 NISTP256")); - key_state->state = libssh2_NB_state_sent; + key_state->state = ssh2_NB_state_sent; } - if(key_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, key_state->request, - key_state->request_len, NULL, 0); + if(key_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, key_state->request, + key_state->request_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, "Unable to send ECDH_INIT"); + ret = ssh2_err(session, rc, "Unable to send ECDH_INIT"); goto ecdh_clean_exit; } - key_state->state = libssh2_NB_state_sent1; + key_state->state = ssh2_NB_state_sent1; } - if(key_state->state == libssh2_NB_state_sent1) { - rc = _libssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, - &key_state->data, &key_state->data_len, - 0, NULL, 0, &key_state->req_state); + if(key_state->state == ssh2_NB_state_sent1) { + rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, + &key_state->data, &key_state->data_len, + 0, NULL, 0, &key_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Timeout waiting for ECDH_REPLY reply"); + ret = ssh2_err(session, rc, + "Timeout waiting for ECDH_REPLY reply"); goto ecdh_clean_exit; } - key_state->state = libssh2_NB_state_sent2; + key_state->state = ssh2_NB_state_sent2; } - if(key_state->state == libssh2_NB_state_sent2) { + if(key_state->state == ssh2_NB_state_sent2) { rc = kex_session_ecdh_curve_type(session->kex->name, &type); if(rc) { - ret = _libssh2_error(session, -1, "Unknown KEX nistp curve type"); + ret = ssh2_err(session, -1, "Unknown KEX nistp curve type"); goto ecdh_clean_exit; } @@ -2277,52 +2246,52 @@ ecdh_clean_exit: static void mlkem_nistp_exchange_state_cleanup( LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state) { - _libssh2_bn_free(exchange_state->k); + ssh2_bn_free(exchange_state->k); exchange_state->k = NULL; if(exchange_state->k_value) { - LIBSSH2_FREE(session, exchange_state->k_value); + SSH2_FREE(session, exchange_state->k_value); exchange_state->k_value = NULL; } - exchange_state->state = libssh2_NB_state_idle; + exchange_state->state = ssh2_NB_state_idle; } static void kex_method_mlkem_nistp_cleanup( LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state) { if(key_state->public_key_oct) { - LIBSSH2_FREE(session, key_state->public_key_oct); + SSH2_FREE(session, key_state->public_key_oct); key_state->public_key_oct = NULL; } if(key_state->private_key) { - LIBSSH2_FREE(session, key_state->private_key); + SSH2_FREE(session, key_state->private_key); key_state->private_key = NULL; } if(key_state->mlkem_public_key) { - _libssh2_explicit_zero(key_state->mlkem_public_key, - LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN); - LIBSSH2_FREE(session, key_state->mlkem_public_key); + ssh2_explicit_zero(key_state->mlkem_public_key, + SSH2_MLKEM_768_PUBLIC_KEY_LEN); + SSH2_FREE(session, key_state->mlkem_public_key); key_state->mlkem_public_key = NULL; } if(key_state->mlkem_private_key) { - _libssh2_explicit_zero(key_state->mlkem_private_key, - LIBSSH2_MLKEM_768_PRIVATE_KEY_LEN); - LIBSSH2_FREE(session, key_state->mlkem_private_key); + ssh2_explicit_zero(key_state->mlkem_private_key, + SSH2_MLKEM_768_PRIVATE_KEY_LEN); + SSH2_FREE(session, key_state->mlkem_private_key); key_state->mlkem_private_key = NULL; } if(key_state->data) { - LIBSSH2_FREE(session, key_state->data); + SSH2_FREE(session, key_state->data); key_state->data = NULL; } - key_state->state = libssh2_NB_state_idle; + key_state->state = ssh2_NB_state_idle; - if(key_state->exchange_state.state != libssh2_NB_state_idle) { + if(key_state->exchange_state.state != ssh2_NB_state_idle) { ecdh_exchange_state_cleanup(session, &key_state->exchange_state); } } @@ -2331,53 +2300,51 @@ static int mlkem_nistp(LIBSSH2_SESSION *session, unsigned char *data, size_t data_len, unsigned char *public_t_key, size_t public_t_key_len, - libssh2_ec_key *private_t_key, + ssh2_ec_key *private_t_key, unsigned char *public_pq_key, unsigned char *private_pq_key, struct kmdhgGPshakex_state *exchange_state) { int ret = 0; int rc, ml_kem_size, sha_algo_value; - libssh2_curve_type type; + ssh2_curve_type type; size_t digest_len, ml_kem_cipher_len, public_pq_key_len; unsigned char *shared_secret = NULL; if(kex_session_hybrid_curve_type(session->kex->name, &type)) { - ret = _libssh2_error(session, -1, - "Unknown KEX hybrid nistp curve type"); + ret = ssh2_err(session, -1, "Unknown KEX hybrid nistp curve type"); goto clean_exit; } switch(type) { - case LIBSSH2_EC_CURVE_NISTP256: + case SSH2_EC_CURVE_NISTP256: digest_len = SHA256_DIGEST_LENGTH; - ml_kem_cipher_len = LIBSSH2_MLKEM_768_CIPHERTEXT; + ml_kem_cipher_len = SSH2_MLKEM_768_CIPHERTEXT; ml_kem_size = 768; - public_pq_key_len = LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN; + public_pq_key_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN; sha_algo_value = 256; break; - case LIBSSH2_EC_CURVE_NISTP384: + case SSH2_EC_CURVE_NISTP384: digest_len = SHA384_DIGEST_LENGTH; - ml_kem_cipher_len = LIBSSH2_MLKEM_1024_CIPHERTEXT; + ml_kem_cipher_len = SSH2_MLKEM_1024_CIPHERTEXT; ml_kem_size = 1024; - public_pq_key_len = LIBSSH2_MLKEM_1024_PUBLIC_KEY_LEN; + public_pq_key_len = SSH2_MLKEM_1024_PUBLIC_KEY_LEN; sha_algo_value = 384; break; default: - ret = _libssh2_error(session, -1, - "Unexpected KEX hybrid nistp curve type"); + ret = ssh2_err(session, -1, "Unexpected KEX hybrid nistp curve type"); goto clean_exit; } - if(exchange_state->state == libssh2_NB_state_idle) { + if(exchange_state->state == ssh2_NB_state_idle) { /* Setup initial values */ - exchange_state->k = _libssh2_bn_init(); + exchange_state->k = ssh2_bn_init(); - exchange_state->state = libssh2_NB_state_created; + exchange_state->state = ssh2_NB_state_created; } - if(exchange_state->state == libssh2_NB_state_created) { + if(exchange_state->state == ssh2_NB_state_created) { /* parse INIT reply data */ unsigned char *server_public_key; size_t shared_secret_len; @@ -2385,8 +2352,8 @@ static int mlkem_nistp(LIBSSH2_SESSION *session, struct string_buf buf; if(!data) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Missing host key data"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Missing host key data"); goto clean_exit; } @@ -2396,156 +2363,150 @@ static int mlkem_nistp(LIBSSH2_SESSION *session, } /* server public key Q_S */ - if(_libssh2_get_string(&buf, &server_public_key, - &server_public_key_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected mlkemnistp key length"); + if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected mlkemnistp key length"); goto clean_exit; } if(server_public_key_len <= ml_kem_cipher_len) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unexpected mlkemnistp server " - "public key length"); + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unexpected mlkemnistp server public key length"); goto clean_exit; } /* server signature */ - if(_libssh2_get_string(&buf, &exchange_state->h_sig, - &(exchange_state->h_sig_len))) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unexpected mlkemnistp server sig length"); + if(ssh2_get_string(&buf, &exchange_state->h_sig, + &(exchange_state->h_sig_len))) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unexpected mlkemnistp server sig length"); goto clean_exit; } exchange_state->k_value_len = digest_len + 4; exchange_state->k_value = - LIBSSH2_ALLOC(session, exchange_state->k_value_len); + SSH2_ALLOC(session, exchange_state->k_value_len); if(!exchange_state->k_value) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate buffer for K"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate buffer for K"); goto clean_exit; } - _libssh2_htonu32(exchange_state->k_value, (uint32_t)digest_len); + ssh2_htonu32(exchange_state->k_value, (uint32_t)digest_len); /* Compute the ecdh shared secret K */ - rc = _libssh2_ecdh_gen_k(&exchange_state->k, private_t_key, - server_public_key + ml_kem_cipher_len, - server_public_key_len - ml_kem_cipher_len); + rc = ssh2_ecdh_gen_k(&exchange_state->k, private_t_key, + server_public_key + ml_kem_cipher_len, + server_public_key_len - ml_kem_cipher_len); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "Unable to create ECDH shared secret"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "Unable to create ECDH shared secret"); goto clean_exit; } - shared_secret_len = LIBSSH2_MLKEM_SHARED_SECRET_LEN + - _libssh2_bn_bytes(exchange_state->k); - shared_secret = LIBSSH2_ALLOC(session, shared_secret_len); + shared_secret_len = SSH2_MLKEM_SHARED_SECRET_LEN + + ssh2_bn_bytes(exchange_state->k); + shared_secret = SSH2_ALLOC(session, shared_secret_len); if(!shared_secret) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate buffer for " - "mlkemnistp shared secret"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate buffer for " + "mlkemnistp shared secret"); goto clean_exit; } /* Compute the ML-KEM shared secret */ - rc = _libssh2_mlkem_get_sk(shared_secret, ml_kem_size, private_pq_key, - server_public_key); + rc = ssh2_mlkem_get_sk(shared_secret, ml_kem_size, private_pq_key, + server_public_key); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "Unable to create mlkem shared secret"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "Unable to create mlkem shared secret"); goto clean_exit; } - if(_libssh2_bn_to_bin(exchange_state->k, - shared_secret + - LIBSSH2_MLKEM_SHARED_SECRET_LEN)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write shared secret"); + if(ssh2_bn_to_bin(exchange_state->k, + shared_secret + SSH2_MLKEM_SHARED_SECRET_LEN)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write shared secret"); goto clean_exit; } /*/ verify hash */ switch(type) { - case LIBSSH2_EC_CURVE_NISTP256: { - libssh2_sha256_ctx k_ctx; - if(!libssh2_sha256_init(&k_ctx)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_INIT, - "kex: failed to initialize hash"); + case SSH2_EC_CURVE_NISTP256: { + ssh2_sha256_ctx k_ctx; + if(!ssh2_sha256_init(&k_ctx)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_INIT, + "kex: failed to initialize hash"); goto clean_exit; } - if(!libssh2_sha256_update(k_ctx, shared_secret, - shared_secret_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_CALC, - "kex: failed to calculate hash"); + if(!ssh2_sha256_update(k_ctx, shared_secret, shared_secret_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC, + "kex: failed to calculate hash"); goto clean_exit; } - if(!libssh2_sha256_final(k_ctx, exchange_state->k_value + 4)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_CALC, - "kex: failed to calculate hash"); + if(!ssh2_sha256_final(k_ctx, exchange_state->k_value + 4)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC, + "kex: failed to calculate hash"); goto clean_exit; } - LIBSSH2_KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(256); + KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(256); break; } - case LIBSSH2_EC_CURVE_NISTP384: { - libssh2_sha384_ctx k_ctx; - if(!libssh2_sha384_init(&k_ctx)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_INIT, - "kex: failed to initialize hash"); + case SSH2_EC_CURVE_NISTP384: { + ssh2_sha384_ctx k_ctx; + if(!ssh2_sha384_init(&k_ctx)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_INIT, + "kex: failed to initialize hash"); goto clean_exit; } - if(!libssh2_sha384_update(k_ctx, shared_secret, - shared_secret_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_CALC, - "kex: failed to calculate hash"); + if(!ssh2_sha384_update(k_ctx, shared_secret, shared_secret_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC, + "kex: failed to calculate hash"); goto clean_exit; } - if(!libssh2_sha384_final(k_ctx, exchange_state->k_value + 4)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_CALC, - "kex: failed to calculate hash"); + if(!ssh2_sha384_final(k_ctx, exchange_state->k_value + 4)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC, + "kex: failed to calculate hash"); goto clean_exit; } - LIBSSH2_KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(384); + KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(384); break; } default: - ret = _libssh2_error(session, -1, - "Unexpected KEX hybrid nistp curve type"); + ret = ssh2_err(session, -1, + "Unexpected KEX hybrid nistp curve type"); goto clean_exit; } if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN, - "Unable to verify hostkey signature " - "mlkemnistp"); + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_SIGN, + "Unable to verify hostkey signature mlkemnistp"); goto clean_exit; } exchange_state->c = SSH_MSG_NEWKEYS; - exchange_state->state = libssh2_NB_state_sent; + exchange_state->state = ssh2_NB_state_sent; } - if(exchange_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); + if(exchange_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send NEWKEYS message mlkemnistp"); + ret = ssh2_err(session, rc, + "Unable to send NEWKEYS message mlkemnistp"); goto clean_exit; } - exchange_state->state = libssh2_NB_state_sent2; + exchange_state->state = ssh2_NB_state_sent2; } - if(exchange_state->state == libssh2_NB_state_sent2) { + if(exchange_state->state == ssh2_NB_state_sent2) { ret = finish_kex(session, exchange_state, (int)digest_len, sha_algo_value); if(ret == LIBSSH2_ERROR_EAGAIN) { @@ -2557,7 +2518,7 @@ clean_exit: mlkem_nistp_exchange_state_cleanup(session, exchange_state); if(shared_secret) { - LIBSSH2_FREE(session, shared_secret); + SSH2_FREE(session, shared_secret); } return ret; @@ -2568,107 +2529,103 @@ static int kex_method_mlkem_nistp_key_exchange( { int ret = 0; int rc = 0; - libssh2_curve_type type; + ssh2_curve_type type; int ml_kem_size; size_t ml_kem_key_len; - if(key_state->state == libssh2_NB_state_idle) { + if(key_state->state == ssh2_NB_state_idle) { key_state->public_key_oct = NULL; - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } - if(key_state->state == libssh2_NB_state_created) { + if(key_state->state == ssh2_NB_state_created) { unsigned char *s = NULL; if(kex_session_hybrid_curve_type(session->kex->name, &type)) { - ret = _libssh2_error(session, -1, - "Unknown KEX hybrid nistp curve type"); + ret = ssh2_err(session, -1, "Unknown KEX hybrid nistp curve type"); goto clean_exit; } switch(type) { - case LIBSSH2_EC_CURVE_NISTP256: + case SSH2_EC_CURVE_NISTP256: ml_kem_size = 768; - ml_kem_key_len = LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN; + ml_kem_key_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN; break; - case LIBSSH2_EC_CURVE_NISTP384: + case SSH2_EC_CURVE_NISTP384: ml_kem_size = 1024; - ml_kem_key_len = LIBSSH2_MLKEM_1024_PUBLIC_KEY_LEN; + ml_kem_key_len = SSH2_MLKEM_1024_PUBLIC_KEY_LEN; break; default: - ret = _libssh2_error(session, -1, - "Unexpected KEX hybrid nistp curve type"); + ret = ssh2_err(session, -1, + "Unexpected KEX hybrid nistp curve type"); goto clean_exit; } - rc = _libssh2_ecdsa_create_key(session, &key_state->private_key, - &key_state->public_key_oct, - &key_state->public_key_oct_len, type); + rc = ssh2_ecdsa_create_key(session, &key_state->private_key, + &key_state->public_key_oct, + &key_state->public_key_oct_len, type); if(rc) { - ret = _libssh2_error(session, rc, - "Unable to create ecdh private key"); + ret = ssh2_err(session, rc, "Unable to create ecdh private key"); goto clean_exit; } - rc = _libssh2_mlkem_new(session, - ml_kem_size, - &key_state->mlkem_public_key, - &key_state->mlkem_private_key); + rc = ssh2_mlkem_new(session, ml_kem_size, + &key_state->mlkem_public_key, + &key_state->mlkem_private_key); if(rc) { - ret = _libssh2_error(session, rc, - "Unable to create mlkem private key"); + ret = ssh2_err(session, rc, "Unable to create mlkem private key"); goto clean_exit; } key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT; s = key_state->request + 1; - _libssh2_store_hybrid_str(&s, - (const char *)key_state->mlkem_public_key, - ml_kem_key_len, - (const char *)key_state->public_key_oct, - key_state->public_key_oct_len); + ssh2_store_hybrid_str(&s, + (const char *)key_state->mlkem_public_key, + ml_kem_key_len, + (const char *)key_state->public_key_oct, + key_state->public_key_oct_len); key_state->request_len = ml_kem_key_len + key_state->public_key_oct_len + 5; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating mlkem-nistp hybrid SHA2")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating mlkem-nistp hybrid SHA2")); - key_state->state = libssh2_NB_state_sent; + key_state->state = ssh2_NB_state_sent; } - if(key_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, key_state->request, - key_state->request_len, NULL, 0); + if(key_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, key_state->request, + key_state->request_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if(rc) { - ret = _libssh2_error(session, rc, "Unable to send ECDH_INIT"); + ret = ssh2_err(session, rc, "Unable to send ECDH_INIT"); goto clean_exit; } - key_state->state = libssh2_NB_state_sent1; + key_state->state = ssh2_NB_state_sent1; } - if(key_state->state == libssh2_NB_state_sent1) { - rc = _libssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, - &key_state->data, &key_state->data_len, - 0, NULL, 0, &key_state->req_state); + if(key_state->state == ssh2_NB_state_sent1) { + rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, + &key_state->data, &key_state->data_len, + 0, NULL, 0, &key_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if(rc) { - ret = _libssh2_error(session, rc, - "Timeout waiting for ECDH_REPLY reply"); + ret = ssh2_err(session, rc, + "Timeout waiting for ECDH_REPLY reply"); goto clean_exit; } - key_state->state = libssh2_NB_state_sent2; + key_state->state = ssh2_NB_state_sent2; } - if(key_state->state == libssh2_NB_state_sent2) { + if(key_state->state == ssh2_NB_state_sent2) { ret = mlkem_nistp(session, key_state->data, key_state->data_len, (unsigned char *)key_state->public_key_oct, key_state->public_key_oct_len, @@ -2696,42 +2653,42 @@ clean_exit: static void curve25519_exchange_state_cleanup( LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state) { - _libssh2_bn_free(exchange_state->k); + ssh2_bn_free(exchange_state->k); exchange_state->k = NULL; if(exchange_state->k_value) { - LIBSSH2_FREE(session, exchange_state->k_value); + SSH2_FREE(session, exchange_state->k_value); exchange_state->k_value = NULL; } - exchange_state->state = libssh2_NB_state_idle; + exchange_state->state = ssh2_NB_state_idle; } static void kex_method_curve25519_cleanup( LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state) { if(key_state->curve25519_public_key) { - _libssh2_explicit_zero(key_state->curve25519_public_key, - LIBSSH2_ED25519_KEY_LEN); - LIBSSH2_FREE(session, key_state->curve25519_public_key); + ssh2_explicit_zero(key_state->curve25519_public_key, + SSH2_ED25519_KEY_LEN); + SSH2_FREE(session, key_state->curve25519_public_key); key_state->curve25519_public_key = NULL; } if(key_state->curve25519_private_key) { - _libssh2_explicit_zero(key_state->curve25519_private_key, - LIBSSH2_ED25519_KEY_LEN); - LIBSSH2_FREE(session, key_state->curve25519_private_key); + ssh2_explicit_zero(key_state->curve25519_private_key, + SSH2_ED25519_KEY_LEN); + SSH2_FREE(session, key_state->curve25519_private_key); key_state->curve25519_private_key = NULL; } if(key_state->data) { - LIBSSH2_FREE(session, key_state->data); + SSH2_FREE(session, key_state->data); key_state->data = NULL; } - key_state->state = libssh2_NB_state_idle; + key_state->state = ssh2_NB_state_idle; - if(key_state->exchange_state.state != libssh2_NB_state_idle) { + if(key_state->exchange_state.state != ssh2_NB_state_idle) { curve25519_exchange_state_cleanup(session, &key_state->exchange_state); } } @@ -2739,34 +2696,33 @@ static void kex_method_curve25519_cleanup( /* * Elliptic Curve Key Exchange */ -static int curve25519_sha256( - LIBSSH2_SESSION *session, unsigned char *data, - size_t data_len, - unsigned char public_key[LIBSSH2_ED25519_KEY_LEN], - unsigned char private_key[LIBSSH2_ED25519_KEY_LEN], - struct kmdhgGPshakex_state *exchange_state) +static int curve25519_sha256(LIBSSH2_SESSION *session, unsigned char *data, + size_t data_len, + unsigned char public_key[SSH2_ED25519_KEY_LEN], + unsigned char private_key[SSH2_ED25519_KEY_LEN], + struct kmdhgGPshakex_state *exchange_state) { int ret = 0; int rc; - int public_key_len = LIBSSH2_ED25519_KEY_LEN; + int public_key_len = SSH2_ED25519_KEY_LEN; - if(exchange_state->state == libssh2_NB_state_idle) { + if(exchange_state->state == ssh2_NB_state_idle) { /* Setup initial values */ - exchange_state->k = _libssh2_bn_init(); + exchange_state->k = ssh2_bn_init(); - exchange_state->state = libssh2_NB_state_created; + exchange_state->state = ssh2_NB_state_created; } - if(exchange_state->state == libssh2_NB_state_created) { + if(exchange_state->state == ssh2_NB_state_created) { /* parse INIT reply data */ unsigned char *server_public_key; size_t server_public_key_len; struct string_buf buf; if(!data) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Missing host key data"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Missing host key data"); goto clean_exit; } @@ -2776,98 +2732,95 @@ static int curve25519_sha256( } /* server public key Q_S */ - if(_libssh2_get_string(&buf, &server_public_key, - &server_public_key_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected curve25519 key length"); + if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected curve25519 key length"); goto clean_exit; } - if(server_public_key_len != LIBSSH2_ED25519_KEY_LEN) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unexpected curve25519 server " - "public key length"); + if(server_public_key_len != SSH2_ED25519_KEY_LEN) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unexpected curve25519 server public key length"); goto clean_exit; } /* server signature */ - if(_libssh2_get_string(&buf, &exchange_state->h_sig, - &(exchange_state->h_sig_len))) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unexpected curve25519 server sig length"); + if(ssh2_get_string(&buf, &exchange_state->h_sig, + &(exchange_state->h_sig_len))) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unexpected curve25519 server sig length"); goto clean_exit; } /* Compute the shared secret K */ - rc = _libssh2_curve25519_gen_k(&exchange_state->k, private_key, - server_public_key); + rc = ssh2_curve25519_gen_k(&exchange_state->k, private_key, + server_public_key); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "Unable to create curve25519 shared secret"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "Unable to create curve25519 shared secret"); goto clean_exit; } - exchange_state->k_value_len = _libssh2_bn_bytes(exchange_state->k) + 5; - if(_libssh2_bn_bits(exchange_state->k) % 8) { + exchange_state->k_value_len = ssh2_bn_bytes(exchange_state->k) + 5; + if(ssh2_bn_bits(exchange_state->k) % 8) { /* do not need leading 00 */ exchange_state->k_value_len--; } exchange_state->k_value = - LIBSSH2_ALLOC(session, exchange_state->k_value_len); + SSH2_ALLOC(session, exchange_state->k_value_len); if(!exchange_state->k_value) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate buffer for K"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate buffer for K"); goto clean_exit; } - _libssh2_htonu32(exchange_state->k_value, - (uint32_t)(exchange_state->k_value_len - 4)); - if(_libssh2_bn_bits(exchange_state->k) % 8) { - if(_libssh2_bn_to_bin(exchange_state->k, - exchange_state->k_value + 4)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->k"); + ssh2_htonu32(exchange_state->k_value, + (uint32_t)(exchange_state->k_value_len - 4)); + if(ssh2_bn_bits(exchange_state->k) % 8) { + if(ssh2_bn_to_bin(exchange_state->k, + exchange_state->k_value + 4)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->k"); goto clean_exit; } } else { exchange_state->k_value[4] = 0; - if(_libssh2_bn_to_bin(exchange_state->k, - exchange_state->k_value + 5)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write exchange_state->k"); + if(ssh2_bn_to_bin(exchange_state->k, + exchange_state->k_value + 5)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write exchange_state->k"); goto clean_exit; } } /*/ verify hash */ - LIBSSH2_KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(256); + KEX_METHOD_EC_SHA_HASH_CREATE_VERIFY(256); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN, - "Unable to verify hostkey signature " - "curve25519"); + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_SIGN, + "Unable to verify hostkey signature curve25519"); goto clean_exit; } exchange_state->c = SSH_MSG_NEWKEYS; - exchange_state->state = libssh2_NB_state_sent; + exchange_state->state = ssh2_NB_state_sent; } - if(exchange_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); + if(exchange_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send NEWKEYS message curve25519"); + ret = ssh2_err(session, rc, + "Unable to send NEWKEYS message curve25519"); goto clean_exit; } - exchange_state->state = libssh2_NB_state_sent2; + exchange_state->state = ssh2_NB_state_sent2; } - if(exchange_state->state == libssh2_NB_state_sent2) { + if(exchange_state->state == ssh2_NB_state_sent2) { ret = finish_kex(session, exchange_state, SHA256_DIGEST_LENGTH, 256); if(ret == LIBSSH2_ERROR_EAGAIN) { return ret; @@ -2889,13 +2842,13 @@ static int kex_method_curve25519_key_exchange( int ret = 0; int rc = 0; - if(key_state->state == libssh2_NB_state_idle) { + if(key_state->state == ssh2_NB_state_idle) { key_state->public_key_oct = NULL; - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } - if(key_state->state == libssh2_NB_state_created) { + if(key_state->state == ssh2_NB_state_created) { unsigned char *s = NULL; rc = strcmp(session->kex->name, "curve25519-sha256@libssh.org"); @@ -2903,63 +2856,61 @@ static int kex_method_curve25519_key_exchange( rc = strcmp(session->kex->name, "curve25519-sha256"); if(rc) { - ret = _libssh2_error(session, -1, - "Unknown KEX curve25519 curve type"); + ret = ssh2_err(session, -1, "Unknown KEX curve25519 curve type"); goto clean_exit; } - rc = _libssh2_curve25519_new(session, - &key_state->curve25519_public_key, - &key_state->curve25519_private_key); + rc = ssh2_curve25519_new(session, + &key_state->curve25519_public_key, + &key_state->curve25519_private_key); if(rc) { - ret = _libssh2_error(session, rc, "Unable to create private key"); + ret = ssh2_err(session, rc, "Unable to create private key"); goto clean_exit; } key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT; s = key_state->request + 1; - _libssh2_store_str(&s, (const char *)key_state->curve25519_public_key, - LIBSSH2_ED25519_KEY_LEN); - key_state->request_len = LIBSSH2_ED25519_KEY_LEN + 5; + ssh2_store_str(&s, (const char *)key_state->curve25519_public_key, + SSH2_ED25519_KEY_LEN); + key_state->request_len = SSH2_ED25519_KEY_LEN + 5; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating curve25519 SHA2")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Initiating curve25519 SHA2")); - key_state->state = libssh2_NB_state_sent; + key_state->state = ssh2_NB_state_sent; } - if(key_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, key_state->request, - key_state->request_len, NULL, 0); + if(key_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, key_state->request, + key_state->request_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, "Unable to send ECDH_INIT"); + ret = ssh2_err(session, rc, "Unable to send ECDH_INIT"); goto clean_exit; } - key_state->state = libssh2_NB_state_sent1; + key_state->state = ssh2_NB_state_sent1; } - if(key_state->state == libssh2_NB_state_sent1) { - rc = _libssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, - &key_state->data, &key_state->data_len, - 0, NULL, 0, &key_state->req_state); + if(key_state->state == ssh2_NB_state_sent1) { + rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, + &key_state->data, &key_state->data_len, + 0, NULL, 0, &key_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Timeout waiting for ECDH_REPLY reply"); + ret = ssh2_err(session, rc, + "Timeout waiting for ECDH_REPLY reply"); goto clean_exit; } - key_state->state = libssh2_NB_state_sent2; + key_state->state = ssh2_NB_state_sent2; } - if(key_state->state == libssh2_NB_state_sent2) { + if(key_state->state == ssh2_NB_state_sent2) { ret = curve25519_sha256(session, key_state->data, key_state->data_len, key_state->curve25519_public_key, @@ -2983,56 +2934,56 @@ clean_exit: static void mlkem768x25519_exchange_state_cleanup( LIBSSH2_SESSION *session, struct kmdhgGPshakex_state *exchange_state) { - _libssh2_bn_free(exchange_state->k); + ssh2_bn_free(exchange_state->k); exchange_state->k = NULL; if(exchange_state->k_value) { - LIBSSH2_FREE(session, exchange_state->k_value); + SSH2_FREE(session, exchange_state->k_value); exchange_state->k_value = NULL; } - exchange_state->state = libssh2_NB_state_idle; + exchange_state->state = ssh2_NB_state_idle; } static void kex_method_mlkem768x25519_cleanup( LIBSSH2_SESSION *session, struct key_exchange_state_low *key_state) { if(key_state->curve25519_public_key) { - _libssh2_explicit_zero(key_state->curve25519_public_key, - LIBSSH2_ED25519_KEY_LEN); - LIBSSH2_FREE(session, key_state->curve25519_public_key); + ssh2_explicit_zero(key_state->curve25519_public_key, + SSH2_ED25519_KEY_LEN); + SSH2_FREE(session, key_state->curve25519_public_key); key_state->curve25519_public_key = NULL; } if(key_state->curve25519_private_key) { - _libssh2_explicit_zero(key_state->curve25519_private_key, - LIBSSH2_ED25519_KEY_LEN); - LIBSSH2_FREE(session, key_state->curve25519_private_key); + ssh2_explicit_zero(key_state->curve25519_private_key, + SSH2_ED25519_KEY_LEN); + SSH2_FREE(session, key_state->curve25519_private_key); key_state->curve25519_private_key = NULL; } if(key_state->mlkem_public_key) { - _libssh2_explicit_zero(key_state->mlkem_public_key, - LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN); - LIBSSH2_FREE(session, key_state->mlkem_public_key); + ssh2_explicit_zero(key_state->mlkem_public_key, + SSH2_MLKEM_768_PUBLIC_KEY_LEN); + SSH2_FREE(session, key_state->mlkem_public_key); key_state->mlkem_public_key = NULL; } if(key_state->mlkem_private_key) { - _libssh2_explicit_zero(key_state->mlkem_private_key, - LIBSSH2_MLKEM_768_PRIVATE_KEY_LEN); - LIBSSH2_FREE(session, key_state->mlkem_private_key); + ssh2_explicit_zero(key_state->mlkem_private_key, + SSH2_MLKEM_768_PRIVATE_KEY_LEN); + SSH2_FREE(session, key_state->mlkem_private_key); key_state->mlkem_private_key = NULL; } if(key_state->data) { - LIBSSH2_FREE(session, key_state->data); + SSH2_FREE(session, key_state->data); key_state->data = NULL; } - key_state->state = libssh2_NB_state_idle; + key_state->state = ssh2_NB_state_idle; - if(key_state->exchange_state.state != libssh2_NB_state_idle) { + if(key_state->exchange_state.state != ssh2_NB_state_idle) { curve25519_exchange_state_cleanup(session, &key_state->exchange_state); } } @@ -3040,32 +2991,32 @@ static void kex_method_mlkem768x25519_cleanup( static int mlkem768x25519_sha256( LIBSSH2_SESSION *session, unsigned char *data, size_t data_len, - unsigned char public_t_key[LIBSSH2_ED25519_KEY_LEN], - unsigned char private_t_key[LIBSSH2_ED25519_KEY_LEN], - unsigned char public_pq_key[LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN], - unsigned char private_pq_key[LIBSSH2_MLKEM_768_PRIVATE_KEY_LEN], + unsigned char public_t_key[SSH2_ED25519_KEY_LEN], + unsigned char private_t_key[SSH2_ED25519_KEY_LEN], + unsigned char public_pq_key[SSH2_MLKEM_768_PUBLIC_KEY_LEN], + unsigned char private_pq_key[SSH2_MLKEM_768_PRIVATE_KEY_LEN], struct kmdhgGPshakex_state *exchange_state) { int ret = 0; int rc; - if(exchange_state->state == libssh2_NB_state_idle) { + if(exchange_state->state == ssh2_NB_state_idle) { /* Setup initial values */ - exchange_state->k = _libssh2_bn_init(); + exchange_state->k = ssh2_bn_init(); - exchange_state->state = libssh2_NB_state_created; + exchange_state->state = ssh2_NB_state_created; } - if(exchange_state->state == libssh2_NB_state_created) { + if(exchange_state->state == ssh2_NB_state_created) { /* parse INIT reply data */ unsigned char *server_public_key; - size_t public_t_key_len = LIBSSH2_ED25519_KEY_LEN; - size_t public_pq_key_len = LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN; - unsigned char shared_secret[LIBSSH2_MLKEM_SHARED_SECRET_LEN + - LIBSSH2_ED25519_KEY_LEN]; + size_t public_t_key_len = SSH2_ED25519_KEY_LEN; + size_t public_pq_key_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN; + unsigned char shared_secret[SSH2_MLKEM_SHARED_SECRET_LEN + + SSH2_ED25519_KEY_LEN]; size_t server_public_key_len; struct string_buf buf; - libssh2_sha256_ctx k_ctx; + ssh2_sha256_ctx k_ctx; ret = process_host_key(session, &buf, exchange_state, data, data_len); if(ret) { @@ -3073,118 +3024,114 @@ static int mlkem768x25519_sha256( } /* server public key Q_S */ - if(_libssh2_get_string(&buf, &server_public_key, - &server_public_key_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected mlkem768x25519 key length"); + if(ssh2_get_string(&buf, &server_public_key, &server_public_key_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected mlkem768x25519 key length"); goto clean_exit; } - if(server_public_key_len != LIBSSH2_MLKEM_768_CIPHERTEXT + - LIBSSH2_ED25519_KEY_LEN) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unexpected mlkem768x25519 server " - "public key length"); + if(server_public_key_len != + SSH2_MLKEM_768_CIPHERTEXT + SSH2_ED25519_KEY_LEN) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unexpected mlkem768x25519 server " + "public key length"); goto clean_exit; } /* server signature */ - if(_libssh2_get_string(&buf, &exchange_state->h_sig, - &(exchange_state->h_sig_len))) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_INIT, - "Unexpected mlkem768x25519 server " - "sig length"); + if(ssh2_get_string(&buf, &exchange_state->h_sig, + &(exchange_state->h_sig_len))) { + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_INIT, + "Unexpected mlkem768x25519 server sig length"); goto clean_exit; } exchange_state->k_value_len = SHA256_DIGEST_LENGTH + 4; exchange_state->k_value = - LIBSSH2_ALLOC(session, exchange_state->k_value_len); + SSH2_ALLOC(session, exchange_state->k_value_len); if(!exchange_state->k_value) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate buffer for K"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate buffer for K"); goto clean_exit; } - _libssh2_htonu32(exchange_state->k_value, SHA256_DIGEST_LENGTH); + ssh2_htonu32(exchange_state->k_value, SHA256_DIGEST_LENGTH); /* Compute the ML-KEM shared secret */ - rc = _libssh2_mlkem_get_sk(shared_secret, 768, - private_pq_key, server_public_key); + rc = ssh2_mlkem_get_sk(shared_secret, 768, + private_pq_key, server_public_key); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "Unable to create mlkem shared secret"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "Unable to create mlkem shared secret"); goto clean_exit; } /* Compute the x2559 shared secret K */ - rc = _libssh2_curve25519_gen_k(&exchange_state->k, private_t_key, - server_public_key + - LIBSSH2_MLKEM_768_CIPHERTEXT); + rc = ssh2_curve25519_gen_k(&exchange_state->k, private_t_key, + server_public_key + + SSH2_MLKEM_768_CIPHERTEXT); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEX_FAILURE, - "Unable to create curve25519 shared secret"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEX_FAILURE, + "Unable to create curve25519 shared secret"); goto clean_exit; } - if(_libssh2_bn_to_bin(exchange_state->k, - shared_secret + - LIBSSH2_MLKEM_SHARED_SECRET_LEN)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Cannot write shared secret"); + if(ssh2_bn_to_bin(exchange_state->k, + shared_secret + SSH2_MLKEM_SHARED_SECRET_LEN)) { + ret = ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Cannot write shared secret"); goto clean_exit; } - if(!libssh2_sha256_init(&k_ctx)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_INIT, - "kex: failed to initialize hash"); + if(!ssh2_sha256_init(&k_ctx)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_INIT, + "kex: failed to initialize hash"); goto clean_exit; } - if(!libssh2_sha256_update(k_ctx, shared_secret, - LIBSSH2_MLKEM_SHARED_SECRET_LEN + - LIBSSH2_ED25519_KEY_LEN)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_CALC, - "kex: failed to calculate hash"); + if(!ssh2_sha256_update(k_ctx, shared_secret, + SSH2_MLKEM_SHARED_SECRET_LEN + + SSH2_ED25519_KEY_LEN)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC, + "kex: failed to calculate hash"); goto clean_exit; } - if(!libssh2_sha256_final(k_ctx, exchange_state->k_value + 4)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HASH_CALC, - "kex: failed to calculate hash"); + if(!ssh2_sha256_final(k_ctx, exchange_state->k_value + 4)) { + ret = ssh2_err(session, LIBSSH2_ERROR_HASH_CALC, + "kex: failed to calculate hash"); goto clean_exit; } /*/ verify hash */ - LIBSSH2_KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(256); + KEX_METHOD_HYBRID_SHA_HASH_CREATE_VERIFY(256); if(rc) { - ret = _libssh2_error(session, LIBSSH2_ERROR_HOSTKEY_SIGN, - "Unable to verify hostkey signature " - "mlkem768x25519"); + ret = ssh2_err(session, LIBSSH2_ERROR_HOSTKEY_SIGN, + "Unable to verify hostkey signature " + "mlkem768x25519"); goto clean_exit; } exchange_state->c = SSH_MSG_NEWKEYS; - exchange_state->state = libssh2_NB_state_sent; + exchange_state->state = ssh2_NB_state_sent; } - if(exchange_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); + if(exchange_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, &exchange_state->c, 1, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - ret = _libssh2_error(session, rc, - "Unable to send NEWKEYS " - "message mlkem768x25519"); + ret = ssh2_err(session, rc, + "Unable to send NEWKEYS message mlkem768x25519"); goto clean_exit; } - exchange_state->state = libssh2_NB_state_sent2; + exchange_state->state = ssh2_NB_state_sent2; } - if(exchange_state->state == libssh2_NB_state_sent2) { + if(exchange_state->state == ssh2_NB_state_sent2) { ret = finish_kex(session, exchange_state, SHA256_DIGEST_LENGTH, 256); if(ret == LIBSSH2_ERROR_EAGAIN) { return ret; @@ -3203,83 +3150,80 @@ static int kex_method_mlkem768x25519_key_exchange( int ret = 0; int rc = 0; - if(key_state->state == libssh2_NB_state_idle) { + if(key_state->state == ssh2_NB_state_idle) { key_state->public_key_oct = NULL; - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } - if(key_state->state == libssh2_NB_state_created) { + if(key_state->state == ssh2_NB_state_created) { unsigned char *s = NULL; - rc = _libssh2_curve25519_new(session, - &key_state->curve25519_public_key, - &key_state->curve25519_private_key); + rc = ssh2_curve25519_new(session, + &key_state->curve25519_public_key, + &key_state->curve25519_private_key); if(rc) { - ret = _libssh2_error(session, rc, - "Unable to create x25519 private key"); + ret = ssh2_err(session, rc, "Unable to create x25519 private key"); goto clean_exit; } - rc = _libssh2_mlkem_new(session, - 768, - &key_state->mlkem_public_key, - &key_state->mlkem_private_key); + rc = ssh2_mlkem_new(session, 768, + &key_state->mlkem_public_key, + &key_state->mlkem_private_key); if(rc) { - ret = _libssh2_error(session, rc, - "Unable to create mlkem private key"); + ret = ssh2_err(session, rc, "Unable to create mlkem private key"); goto clean_exit; } key_state->request[0] = SSH2_MSG_KEX_ECDH_INIT; s = key_state->request + 1; - _libssh2_store_hybrid_str(&s, - (const char *)key_state->mlkem_public_key, - LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN, - (const char *)key_state->curve25519_public_key, - LIBSSH2_ED25519_KEY_LEN); - key_state->request_len = LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN + - LIBSSH2_ED25519_KEY_LEN + 5; + ssh2_store_hybrid_str(&s, + (const char *)key_state->mlkem_public_key, + SSH2_MLKEM_768_PUBLIC_KEY_LEN, + (const char *)key_state->curve25519_public_key, + SSH2_ED25519_KEY_LEN); + key_state->request_len = SSH2_MLKEM_768_PUBLIC_KEY_LEN + + SSH2_ED25519_KEY_LEN + 5; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Initiating mlkem768x25519 SHA2")); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Initiating mlkem768x25519 SHA2")); - key_state->state = libssh2_NB_state_sent; + key_state->state = ssh2_NB_state_sent; } - if(key_state->state == libssh2_NB_state_sent) { - rc = _libssh2_transport_send(session, key_state->request, - key_state->request_len, NULL, 0); + if(key_state->state == ssh2_NB_state_sent) { + rc = ssh2_transport_send(session, key_state->request, + key_state->request_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if(rc) { - ret = _libssh2_error(session, rc, "Unable to send ECDH_INIT"); + ret = ssh2_err(session, rc, "Unable to send ECDH_INIT"); goto clean_exit; } - key_state->state = libssh2_NB_state_sent1; + key_state->state = ssh2_NB_state_sent1; } - if(key_state->state == libssh2_NB_state_sent1) { - rc = _libssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, - &key_state->data, &key_state->data_len, 0, - NULL, 0, &key_state->req_state); + if(key_state->state == ssh2_NB_state_sent1) { + rc = ssh2_packet_require(session, SSH2_MSG_KEX_ECDH_REPLY, + &key_state->data, &key_state->data_len, 0, + NULL, 0, &key_state->req_state); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } if(rc) { - ret = _libssh2_error(session, rc, - "Timeout waiting for ECDH_REPLY reply"); + ret = ssh2_err(session, rc, + "Timeout waiting for ECDH_REPLY reply"); goto clean_exit; } - key_state->state = libssh2_NB_state_sent2; + key_state->state = ssh2_NB_state_sent2; } - if(key_state->state == libssh2_NB_state_sent2) { + if(key_state->state == ssh2_NB_state_sent2) { ret = mlkem768x25519_sha256(session, key_state->data, key_state->data_len, @@ -3304,42 +3248,42 @@ clean_exit: #endif /* LIBSSH2_ED25519 */ -#define LIBSSH2_KEX_METHOD_FLAG_REQ_ENC_HOSTKEY 0x0001 -#define LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY 0x0002 +#define KEX_METHOD_FLAG_REQ_ENC_HOSTKEY 0x0001 +#define KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY 0x0002 static const struct kex_method kex_method_diffie_hellman_group1_sha1 = { "diffie-hellman-group1-sha1", kex_method_diffie_hellman_group1_sha1_key_exchange, kex_diffie_hellman_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_diffie_hellman_group14_sha1 = { "diffie-hellman-group14-sha1", kex_method_diffie_hellman_group14_sha1_key_exchange, kex_diffie_hellman_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_diffie_hellman_group14_sha256 = { "diffie-hellman-group14-sha256", kex_method_diffie_hellman_group14_sha256_key_exchange, kex_diffie_hellman_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_diffie_hellman_group16_sha512 = { "diffie-hellman-group16-sha512", kex_method_diffie_hellman_group16_sha512_key_exchange, kex_diffie_hellman_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_diffie_hellman_group18_sha512 = { "diffie-hellman-group18-sha512", kex_method_diffie_hellman_group18_sha512_key_exchange, kex_diffie_hellman_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method @@ -3347,7 +3291,7 @@ kex_method_diffie_hellman_group_exchange_sha1 = { "diffie-hellman-group-exchange-sha1", kex_method_diffie_hellman_group_exchange_sha1_key_exchange, kex_diffie_hellman_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method @@ -3355,7 +3299,7 @@ kex_method_diffie_hellman_group_exchange_sha256 = { "diffie-hellman-group-exchange-sha256", kex_method_diffie_hellman_group_exchange_sha256_key_exchange, kex_diffie_hellman_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; #if LIBSSH2_ECDSA @@ -3363,34 +3307,34 @@ static const struct kex_method kex_method_ecdh_sha2_nistp256 = { "ecdh-sha2-nistp256", kex_method_ecdh_key_exchange, kex_method_ecdh_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_ecdh_sha2_nistp384 = { "ecdh-sha2-nistp384", kex_method_ecdh_key_exchange, kex_method_ecdh_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_ecdh_sha2_nistp521 = { "ecdh-sha2-nistp521", kex_method_ecdh_key_exchange, kex_method_ecdh_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; #if LIBSSH2_MLKEM static const struct kex_method kex_method_ssh_mlkem768_nistp256_sha256 = { "mlkem768nistp256-sha256", kex_method_mlkem_nistp_key_exchange, kex_method_mlkem_nistp_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_ssh_mlkem1024_nistp384_sha384 = { "mlkem1024nistp384-sha384", kex_method_mlkem_nistp_key_exchange, kex_method_mlkem_nistp_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; #endif #endif @@ -3400,20 +3344,20 @@ static const struct kex_method kex_method_ssh_curve25519_sha256_libssh = { "curve25519-sha256@libssh.org", kex_method_curve25519_key_exchange, kex_method_curve25519_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; static const struct kex_method kex_method_ssh_curve25519_sha256 = { "curve25519-sha256", kex_method_curve25519_key_exchange, kex_method_curve25519_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; #if LIBSSH2_MLKEM static const struct kex_method kex_method_ssh_mlkem768_x25519_sha256 = { "mlkem768x25519-sha256", kex_method_mlkem768x25519_key_exchange, kex_method_mlkem768x25519_cleanup, - LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, + KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY, }; #endif #endif @@ -3499,7 +3443,7 @@ static size_t kex_method_strlen(const struct common_method **method) static uint32_t kex_method_list(unsigned char *buf, uint32_t list_strlen, const struct common_method **method) { - _libssh2_htonu32(buf, list_strlen); + ssh2_htonu32(buf, list_strlen); buf += 4; if(!method || !*method) { @@ -3517,14 +3461,14 @@ static uint32_t kex_method_list(unsigned char *buf, uint32_t list_strlen, return list_strlen + 4; } -#define LIBSSH2_METHOD_PREFS_LEN(prefvar, defaultvar) \ - (uint32_t)((prefvar) ? strlen(prefvar) : \ +#define KEX_METHOD_PREFS_LEN(prefvar, defaultvar) \ + (uint32_t)((prefvar) ? strlen(prefvar) : \ kex_method_strlen((const struct common_method **)(defaultvar))) -#define LIBSSH2_METHOD_PREFS_STR(buf, prefvarlen, prefvar, defaultvar) \ +#define KEX_METHOD_PREFS_STR(buf, prefvarlen, prefvar, defaultvar) \ do { \ if(prefvar) { \ - _libssh2_htonu32(buf, prefvarlen); \ + ssh2_htonu32(buf, prefvarlen); \ (buf) += 4; \ memcpy(buf, prefvar, prefvarlen); \ (buf) += (prefvarlen); \ @@ -3546,75 +3490,70 @@ static int kexinit(LIBSSH2_SESSION *session) unsigned char *data, *s; int rc; - if(session->kexinit_state == libssh2_NB_state_idle) { + if(session->kexinit_state == ssh2_NB_state_idle) { uint32_t kex_len, hostkey_len; uint32_t crypt_cs_len, crypt_sc_len; uint32_t comp_cs_len, comp_sc_len; uint32_t mac_cs_len, mac_sc_len; uint32_t lang_cs_len, lang_sc_len; - kex_len = LIBSSH2_METHOD_PREFS_LEN(session->kex_prefs, kex_methods); - hostkey_len = LIBSSH2_METHOD_PREFS_LEN(session->hostkey_prefs, - libssh2_hostkey_methods()); - crypt_cs_len = LIBSSH2_METHOD_PREFS_LEN(session->local.crypt_prefs, - libssh2_crypt_methods()); - crypt_sc_len = LIBSSH2_METHOD_PREFS_LEN(session->remote.crypt_prefs, - libssh2_crypt_methods()); - mac_cs_len = LIBSSH2_METHOD_PREFS_LEN(session->local.mac_prefs, - _libssh2_mac_methods()); - mac_sc_len = LIBSSH2_METHOD_PREFS_LEN(session->remote.mac_prefs, - _libssh2_mac_methods()); - comp_cs_len = LIBSSH2_METHOD_PREFS_LEN(session->local.comp_prefs, - _libssh2_comp_methods(session)); - comp_sc_len = LIBSSH2_METHOD_PREFS_LEN(session->remote.comp_prefs, - _libssh2_comp_methods(session)); - lang_cs_len = - LIBSSH2_METHOD_PREFS_LEN(session->local.lang_prefs, NULL); - lang_sc_len = - LIBSSH2_METHOD_PREFS_LEN(session->remote.lang_prefs, NULL); + kex_len = KEX_METHOD_PREFS_LEN(session->kex_prefs, kex_methods); + hostkey_len = KEX_METHOD_PREFS_LEN(session->hostkey_prefs, + ssh2_hostkey_methods()); + crypt_cs_len = KEX_METHOD_PREFS_LEN(session->local.crypt_prefs, + ssh2_crypt_methods()); + crypt_sc_len = KEX_METHOD_PREFS_LEN(session->remote.crypt_prefs, + ssh2_crypt_methods()); + mac_cs_len = KEX_METHOD_PREFS_LEN(session->local.mac_prefs, + ssh2_mac_methods()); + mac_sc_len = KEX_METHOD_PREFS_LEN(session->remote.mac_prefs, + ssh2_mac_methods()); + comp_cs_len = KEX_METHOD_PREFS_LEN(session->local.comp_prefs, + ssh2_comp_methods(session)); + comp_sc_len = KEX_METHOD_PREFS_LEN(session->remote.comp_prefs, + ssh2_comp_methods(session)); + lang_cs_len = KEX_METHOD_PREFS_LEN(session->local.lang_prefs, NULL); + lang_sc_len = KEX_METHOD_PREFS_LEN(session->remote.lang_prefs, NULL); data_len += kex_len + hostkey_len + crypt_cs_len + crypt_sc_len + comp_cs_len + comp_sc_len + mac_cs_len + mac_sc_len + lang_cs_len + lang_sc_len; - s = data = LIBSSH2_ALLOC(session, data_len); + s = data = SSH2_ALLOC(session, data_len); if(!data) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory"); } *(s++) = SSH_MSG_KEXINIT; - if(_libssh2_random(s, 16)) { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_RANDGEN, - "Unable to get random bytes " - "for KEXINIT cookie"); + if(ssh2_random(s, 16)) { + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_RANDGEN, + "Unable to get random bytes for KEXINIT cookie"); } s += 16; /* Ennumerating through these lists twice is probably (certainly?) inefficient from a CPU standpoint, but it saves multiple malloc/realloc calls */ - LIBSSH2_METHOD_PREFS_STR(s, kex_len, session->kex_prefs, kex_methods); - LIBSSH2_METHOD_PREFS_STR(s, hostkey_len, session->hostkey_prefs, - libssh2_hostkey_methods()); - LIBSSH2_METHOD_PREFS_STR(s, crypt_cs_len, session->local.crypt_prefs, - libssh2_crypt_methods()); - LIBSSH2_METHOD_PREFS_STR(s, crypt_sc_len, session->remote.crypt_prefs, - libssh2_crypt_methods()); - LIBSSH2_METHOD_PREFS_STR(s, mac_cs_len, session->local.mac_prefs, - _libssh2_mac_methods()); - LIBSSH2_METHOD_PREFS_STR(s, mac_sc_len, session->remote.mac_prefs, - _libssh2_mac_methods()); - LIBSSH2_METHOD_PREFS_STR(s, comp_cs_len, session->local.comp_prefs, - _libssh2_comp_methods(session)); - LIBSSH2_METHOD_PREFS_STR(s, comp_sc_len, session->remote.comp_prefs, - _libssh2_comp_methods(session)); - LIBSSH2_METHOD_PREFS_STR(s, lang_cs_len, session->local.lang_prefs, - NULL); - LIBSSH2_METHOD_PREFS_STR(s, lang_sc_len, session->remote.lang_prefs, - NULL); + KEX_METHOD_PREFS_STR(s, kex_len, session->kex_prefs, kex_methods); + KEX_METHOD_PREFS_STR(s, hostkey_len, session->hostkey_prefs, + ssh2_hostkey_methods()); + KEX_METHOD_PREFS_STR(s, crypt_cs_len, session->local.crypt_prefs, + ssh2_crypt_methods()); + KEX_METHOD_PREFS_STR(s, crypt_sc_len, session->remote.crypt_prefs, + ssh2_crypt_methods()); + KEX_METHOD_PREFS_STR(s, mac_cs_len, session->local.mac_prefs, + ssh2_mac_methods()); + KEX_METHOD_PREFS_STR(s, mac_sc_len, session->remote.mac_prefs, + ssh2_mac_methods()); + KEX_METHOD_PREFS_STR(s, comp_cs_len, session->local.comp_prefs, + ssh2_comp_methods(session)); + KEX_METHOD_PREFS_STR(s, comp_sc_len, session->remote.comp_prefs, + ssh2_comp_methods(session)); + KEX_METHOD_PREFS_STR(s, lang_cs_len, session->local.lang_prefs, NULL); + KEX_METHOD_PREFS_STR(s, lang_sc_len, session->remote.lang_prefs, NULL); /* No optimistic KEX packet follows */ /* Deal with optimistic packets @@ -3624,48 +3563,48 @@ static int kexinit(LIBSSH2_SESSION *session) *(s++) = 0; /* Reserved == 0 */ - _libssh2_htonu32(s, 0); + ssh2_htonu32(s, 0); #ifdef LIBSSH2DEBUG { unsigned char *p = data + 21; /* type(1) + cookie(16) + len(4) */ - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent KEX: %.*s", (int)kex_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent KEX: %.*s", + (int)kex_len, p)); p += kex_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent HOSTKEY: %.*s", (int)hostkey_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent HOSTKEY: %.*s", + (int)hostkey_len, p)); p += hostkey_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent CRYPT_CS: %.*s", (int)crypt_cs_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent CRYPT_CS: %.*s", + (int)crypt_cs_len, p)); p += crypt_cs_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent CRYPT_SC: %.*s", (int)crypt_sc_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent CRYPT_SC: %.*s", + (int)crypt_sc_len, p)); p += crypt_sc_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent MAC_CS: %.*s", (int)mac_cs_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent MAC_CS: %.*s", + (int)mac_cs_len, p)); p += mac_cs_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent MAC_SC: %.*s", (int)mac_sc_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent MAC_SC: %.*s", + (int)mac_sc_len, p)); p += mac_sc_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent COMP_CS: %.*s", (int)comp_cs_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent COMP_CS: %.*s", + (int)comp_cs_len, p)); p += comp_cs_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent COMP_SC: %.*s", (int)comp_sc_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent COMP_SC: %.*s", + (int)comp_sc_len, p)); p += comp_sc_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent LANG_CS: %.*s", (int)lang_cs_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent LANG_CS: %.*s", + (int)lang_cs_len, p)); p += lang_cs_len + 4; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent LANG_SC: %.*s", (int)lang_sc_len, p)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Sent LANG_SC: %.*s", + (int)lang_sc_len, p)); p += lang_sc_len; - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Sent first_kex_packet_follows: %02x", p[0])); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Sent first_kex_packet_follows: %02x", p[0])); } #endif /* LIBSSH2DEBUG */ - session->kexinit_state = libssh2_NB_state_created; + session->kexinit_state = ssh2_NB_state_created; } else { data = session->kexinit_data; @@ -3675,27 +3614,27 @@ static int kexinit(LIBSSH2_SESSION *session) session->kexinit_data_len = 0; } - rc = _libssh2_transport_send(session, data, data_len, NULL, 0); + rc = ssh2_transport_send(session, data, data_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { session->kexinit_data = data; session->kexinit_data_len = data_len; return rc; } else if(rc) { - LIBSSH2_FREE(session, data); - session->kexinit_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Unable to send KEXINIT packet to remote host"); + SSH2_FREE(session, data); + session->kexinit_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, + "Unable to send KEXINIT packet to remote host"); } if(session->local.kexinit) { - LIBSSH2_FREE(session, session->local.kexinit); + SSH2_FREE(session, session->local.kexinit); } session->local.kexinit = data; session->local.kexinit_len = data_len; - session->kexinit_state = libssh2_NB_state_idle; + session->kexinit_state = ssh2_NB_state_idle; return 0; } @@ -3704,10 +3643,10 @@ static int kexinit(LIBSSH2_SESSION *session) * Kex specific variant of strstr() * Needle must be preceded by BOL or ',', and followed by ',' or EOL */ -unsigned char *_libssh2_kex_agree_instr(unsigned char *haystack, - size_t haystack_len, - const unsigned char *needle, - size_t needle_len) +unsigned char *ssh2_kex_agree_instr(unsigned char *haystack, + size_t haystack_len, + const unsigned char *needle, + size_t needle_len) { unsigned char *s; unsigned char *end_haystack; @@ -3778,7 +3717,7 @@ static int kex_agree_hostkey(LIBSSH2_SESSION *session, size_t kex_flags, unsigned char *hostkey, size_t hostkey_len) { - const struct hostkey_method **hostkeyp = libssh2_hostkey_methods(); + const struct hostkey_method **hostkeyp = ssh2_hostkey_methods(); unsigned char *s; if(session->hostkey_prefs) { @@ -3787,7 +3726,7 @@ static int kex_agree_hostkey(LIBSSH2_SESSION *session, while(s && *s) { unsigned char *p = (unsigned char *)strchr((char *)s, ','); size_t method_len = (p ? (size_t)(p - s) : strlen((char *)s)); - if(_libssh2_kex_agree_instr(hostkey, hostkey_len, s, method_len)) { + if(ssh2_kex_agree_instr(hostkey, hostkey_len, s, method_len)) { const struct hostkey_method *method = (const struct hostkey_method *)kex_get_method_by_name( (char *)s, method_len, @@ -3800,12 +3739,12 @@ static int kex_agree_hostkey(LIBSSH2_SESSION *session, /* OK so far, but does it suit our purposes? (Encrypting vs Signing) */ - if(((kex_flags & LIBSSH2_KEX_METHOD_FLAG_REQ_ENC_HOSTKEY) == - 0) || (method->encrypt)) { + if(((kex_flags & KEX_METHOD_FLAG_REQ_ENC_HOSTKEY) == 0) || + method->encrypt) { /* Either this hostkey can do encryption or this kex does not require it */ - if(((kex_flags & LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY) - == 0) || (method->sig_verify)) { + if(((kex_flags & KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY) == 0) || + method->sig_verify) { /* Either this hostkey can do signing or this kex does not require it */ session->hostkey = method; @@ -3820,18 +3759,18 @@ static int kex_agree_hostkey(LIBSSH2_SESSION *session, } while(hostkeyp && (*hostkeyp) && (*hostkeyp)->name) { - s = _libssh2_kex_agree_instr(hostkey, hostkey_len, - (const unsigned char *)(*hostkeyp)->name, - strlen((*hostkeyp)->name)); + s = ssh2_kex_agree_instr(hostkey, hostkey_len, + (const unsigned char *)(*hostkeyp)->name, + strlen((*hostkeyp)->name)); if(s) { /* OK so far, but does it suit our purposes? (Encrypting vs Signing) */ - if(((kex_flags & LIBSSH2_KEX_METHOD_FLAG_REQ_ENC_HOSTKEY) == 0) || - ((*hostkeyp)->encrypt)) { + if(((kex_flags & KEX_METHOD_FLAG_REQ_ENC_HOSTKEY) == 0) || + (*hostkeyp)->encrypt) { /* Either this hostkey can do encryption or this kex does not require it */ - if(((kex_flags & LIBSSH2_KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY) == - 0) || ((*hostkeyp)->sig_verify)) { + if(((kex_flags & KEX_METHOD_FLAG_REQ_SIGN_HOSTKEY) == 0) || + (*hostkeyp)->sig_verify) { /* Either this hostkey can do signing or this kex does not require it */ session->hostkey = *hostkeyp; @@ -3857,7 +3796,7 @@ static int kex_agree_kex_hostkey(LIBSSH2_SESSION *session, unsigned char *kex, const unsigned char *strict = (const unsigned char *)"kex-strict-s-v00@openssh.com"; - if(_libssh2_kex_agree_instr(kex, kex_len, strict, 28)) { + if(ssh2_kex_agree_instr(kex, kex_len, strict, 28)) { session->kex_strict = 1; } @@ -3867,7 +3806,7 @@ static int kex_agree_kex_hostkey(LIBSSH2_SESSION *session, unsigned char *kex, while(s && *s) { unsigned char *q, *p = (unsigned char *)strchr((char *)s, ','); size_t method_len = (p ? (size_t)(p - s) : strlen((char *)s)); - q = _libssh2_kex_agree_instr(kex, kex_len, s, method_len); + q = ssh2_kex_agree_instr(kex, kex_len, s, method_len); if(q) { const struct kex_method *method = (const struct kex_method *)kex_get_method_by_name( @@ -3901,9 +3840,9 @@ static int kex_agree_kex_hostkey(LIBSSH2_SESSION *session, unsigned char *kex, } while(*kexp && (*kexp)->name) { - s = _libssh2_kex_agree_instr(kex, kex_len, - (const unsigned char *)(*kexp)->name, - strlen((*kexp)->name)); + s = ssh2_kex_agree_instr(kex, kex_len, + (const unsigned char *)(*kexp)->name, + strlen((*kexp)->name)); if(s) { /* We have agreed on a key exchange method, * Can we agree on a hostkey that works with this kex? @@ -3933,7 +3872,7 @@ static int kex_agree_crypt(LIBSSH2_SESSION *session, unsigned char *crypt, size_t crypt_len) { - const struct crypt_method **cryptp = libssh2_crypt_methods(); + const struct crypt_method **cryptp = ssh2_crypt_methods(); unsigned char *s; (void)session; @@ -3945,7 +3884,7 @@ static int kex_agree_crypt(LIBSSH2_SESSION *session, unsigned char *p = (unsigned char *)strchr((char *)s, ','); size_t method_len = (p ? (size_t)(p - s) : strlen((char *)s)); - if(_libssh2_kex_agree_instr(crypt, crypt_len, s, method_len)) { + if(ssh2_kex_agree_instr(crypt, crypt_len, s, method_len)) { const struct crypt_method *method = (const struct crypt_method *)kex_get_method_by_name( (char *)s, method_len, @@ -3966,9 +3905,9 @@ static int kex_agree_crypt(LIBSSH2_SESSION *session, } while(*cryptp && (*cryptp)->name) { - s = _libssh2_kex_agree_instr(crypt, crypt_len, - (const unsigned char *)(*cryptp)->name, - strlen((*cryptp)->name)); + s = ssh2_kex_agree_instr(crypt, crypt_len, + (const unsigned char *)(*cryptp)->name, + strlen((*cryptp)->name)); if(s) { endpoint->crypt = *cryptp; return 0; @@ -3986,12 +3925,12 @@ static int kex_agree_mac(LIBSSH2_SESSION *session, struct endpoint_data *endpoint, unsigned char *mac, size_t mac_len) { - const struct mac_method **macp = _libssh2_mac_methods(); + const struct mac_method **macp = ssh2_mac_methods(); const struct mac_method *override; unsigned char *s; (void)session; - override = _libssh2_mac_override(endpoint->crypt); + override = ssh2_mac_override(endpoint->crypt); if(override) { /* This crypto method has its own hmac method built-in, so a separate * negotiation (and use) of a separate hmac method is unnecessary */ @@ -4006,7 +3945,7 @@ static int kex_agree_mac(LIBSSH2_SESSION *session, unsigned char *p = (unsigned char *)strchr((char *)s, ','); size_t method_len = (p ? (size_t)(p - s) : strlen((char *)s)); - if(_libssh2_kex_agree_instr(mac, mac_len, s, method_len)) { + if(ssh2_kex_agree_instr(mac, mac_len, s, method_len)) { const struct mac_method *method = (const struct mac_method *)kex_get_method_by_name( (char *)s, method_len, @@ -4027,9 +3966,9 @@ static int kex_agree_mac(LIBSSH2_SESSION *session, } while(*macp && (*macp)->name) { - s = _libssh2_kex_agree_instr(mac, mac_len, - (const unsigned char *)(*macp)->name, - strlen((*macp)->name)); + s = ssh2_kex_agree_instr(mac, mac_len, + (const unsigned char *)(*macp)->name, + strlen((*macp)->name)); if(s) { endpoint->mac = *macp; return 0; @@ -4047,7 +3986,7 @@ static int kex_agree_comp(LIBSSH2_SESSION *session, struct endpoint_data *endpoint, unsigned char *comp, size_t comp_len) { - const struct comp_method **compp = _libssh2_comp_methods(session); + const struct comp_method **compp = ssh2_comp_methods(session); unsigned char *s; (void)session; @@ -4058,7 +3997,7 @@ static int kex_agree_comp(LIBSSH2_SESSION *session, unsigned char *p = (unsigned char *)strchr((char *)s, ','); size_t method_len = (p ? (size_t)(p - s) : strlen((char *)s)); - if(_libssh2_kex_agree_instr(comp, comp_len, s, method_len)) { + if(ssh2_kex_agree_instr(comp, comp_len, s, method_len)) { const struct comp_method *method = (const struct comp_method *)kex_get_method_by_name( (char *)s, method_len, @@ -4079,9 +4018,9 @@ static int kex_agree_comp(LIBSSH2_SESSION *session, } while(*compp && (*compp)->name) { - s = _libssh2_kex_agree_instr(comp, comp_len, - (const unsigned char *)(*compp)->name, - strlen((*compp)->name)); + s = ssh2_kex_agree_instr(comp, comp_len, + (const unsigned char *)(*compp)->name, + strlen((*compp)->name)); if(s) { endpoint->comp = *compp; return 0; @@ -4120,27 +4059,27 @@ static int kex_agree_methods(LIBSSH2_SESSION *session, unsigned char *data, buf.dataptr += 16; /* Locate each string */ - if(_libssh2_get_string(&buf, &kex, &kex_len)) + if(ssh2_get_string(&buf, &kex, &kex_len)) return -1; - if(_libssh2_get_string(&buf, &hostkey, &hostkey_len)) + if(ssh2_get_string(&buf, &hostkey, &hostkey_len)) return -1; - if(_libssh2_get_string(&buf, &crypt_cs, &crypt_cs_len)) + if(ssh2_get_string(&buf, &crypt_cs, &crypt_cs_len)) return -1; - if(_libssh2_get_string(&buf, &crypt_sc, &crypt_sc_len)) + if(ssh2_get_string(&buf, &crypt_sc, &crypt_sc_len)) return -1; - if(_libssh2_get_string(&buf, &mac_cs, &mac_cs_len)) + if(ssh2_get_string(&buf, &mac_cs, &mac_cs_len)) return -1; - if(_libssh2_get_string(&buf, &mac_sc, &mac_sc_len)) + if(ssh2_get_string(&buf, &mac_sc, &mac_sc_len)) return -1; - if(_libssh2_get_string(&buf, &comp_cs, &comp_cs_len)) + if(ssh2_get_string(&buf, &comp_cs, &comp_cs_len)) return -1; - if(_libssh2_get_string(&buf, &comp_sc, &comp_sc_len)) + if(ssh2_get_string(&buf, &comp_sc, &comp_sc_len)) return -1; /* If the server sent an optimistic packet, assume that it guessed wrong. * If the guess is determined to be right (by kex_agree_kex_hostkey) * This flag is reset to zero so that it is not ignored */ - if(_libssh2_check_length(&buf, 1)) { + if(ssh2_check_length(&buf, 1)) { session->burn_optimistic_kexinit = *(buf.dataptr++); } else { @@ -4171,38 +4110,28 @@ static int kex_agree_methods(LIBSSH2_SESSION *session, unsigned char *data, } #if 0 - if(libssh2_kex_agree_lang(session, &session->local, lang_cs, - lang_cs_len) || - libssh2_kex_agree_lang(session, &session->remote, lang_sc, - lang_sc_len)) { + if(kex_agree_lang(session, &session->local, lang_cs, lang_cs_len) || + kex_agree_lang(session, &session->remote, lang_sc, lang_sc_len)) { return -1; } #endif - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on KEX method: %s", - session->kex->name)); - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on HOSTKEY method: %s", - session->hostkey->name)); - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on CRYPT_CS method: %s", - session->local.crypt->name)); - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on CRYPT_SC method: %s", - session->remote.crypt->name)); - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on MAC_CS method: %s", - session->local.mac->name)); - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on MAC_SC method: %s", - session->remote.mac->name)); - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on COMP_CS method: %s", - session->local.comp->name)); - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Agreed on COMP_SC method: %s", - session->remote.comp->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on KEX method: %s", + session->kex->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on HOSTKEY method: %s", + session->hostkey->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on CRYPT_CS method: %s", + session->local.crypt->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on CRYPT_SC method: %s", + session->remote.crypt->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on MAC_CS method: %s", + session->local.mac->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on MAC_SC method: %s", + session->remote.mac->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on COMP_CS method: %s", + session->local.comp->name)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Agreed on COMP_SC method: %s", + session->remote.comp->name)); return 0; } @@ -4211,19 +4140,19 @@ static int kex_agree_methods(LIBSSH2_SESSION *session, unsigned char *data, * Exchange keys * Returns 0 on success, non-zero on failure * - * Returns some errors without _libssh2_error() + * Returns some errors without ssh2_err() */ -int _libssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange, - struct key_exchange_state *key_state) +int ssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange, + struct key_exchange_state *key_state) { int rc = 0; int retcode; - session->state |= LIBSSH2_STATE_KEX_ACTIVE; + session->state |= SSH2_STATE_KEX_ACTIVE; - if(key_state->state == libssh2_NB_state_idle) { + if(key_state->state == ssh2_NB_state_idle) { /* Prevent loop in packet_add() */ - session->state |= LIBSSH2_STATE_EXCHANGING_KEYS; + session->state |= SSH2_STATE_EXCHANGING_KEYS; if(reexchange) { if(session->kex && session->kex->cleanup) { @@ -4239,64 +4168,64 @@ int _libssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange, session->hostkey = NULL; } - key_state->state = libssh2_NB_state_created; + key_state->state = ssh2_NB_state_created; } if(!session->kex || !session->hostkey) { - if(key_state->state == libssh2_NB_state_created) { + if(key_state->state == ssh2_NB_state_created) { /* Preserve in case of failure */ key_state->oldlocal = session->local.kexinit; key_state->oldlocal_len = session->local.kexinit_len; session->local.kexinit = NULL; - key_state->state = libssh2_NB_state_sent; + key_state->state = ssh2_NB_state_sent; } - if(key_state->state == libssh2_NB_state_sent) { + if(key_state->state == ssh2_NB_state_sent) { retcode = kexinit(session); if(retcode == LIBSSH2_ERROR_EAGAIN) { - session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; + session->state &= ~SSH2_STATE_KEX_ACTIVE; return retcode; } else if(retcode) { session->local.kexinit = key_state->oldlocal; session->local.kexinit_len = key_state->oldlocal_len; - key_state->state = libssh2_NB_state_idle; - session->state &= ~LIBSSH2_STATE_INITIAL_KEX; - session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; - session->state &= ~LIBSSH2_STATE_EXCHANGING_KEYS; + key_state->state = ssh2_NB_state_idle; + session->state &= ~SSH2_STATE_INITIAL_KEX; + session->state &= ~SSH2_STATE_KEX_ACTIVE; + session->state &= ~SSH2_STATE_EXCHANGING_KEYS; return -1; } - key_state->state = libssh2_NB_state_sent1; + key_state->state = ssh2_NB_state_sent1; } - if(key_state->state == libssh2_NB_state_sent1) { + if(key_state->state == ssh2_NB_state_sent1) { retcode = - _libssh2_packet_require(session, SSH_MSG_KEXINIT, - &key_state->data, - &key_state->data_len, 0, NULL, 0, - &key_state->req_state); + ssh2_packet_require(session, SSH_MSG_KEXINIT, + &key_state->data, + &key_state->data_len, 0, NULL, 0, + &key_state->req_state); if(retcode == LIBSSH2_ERROR_EAGAIN) { - session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; + session->state &= ~SSH2_STATE_KEX_ACTIVE; return retcode; } else if(retcode) { if(session->local.kexinit) { - LIBSSH2_FREE(session, session->local.kexinit); + SSH2_FREE(session, session->local.kexinit); } session->local.kexinit = key_state->oldlocal; session->local.kexinit_len = key_state->oldlocal_len; - key_state->state = libssh2_NB_state_idle; - session->state &= ~LIBSSH2_STATE_INITIAL_KEX; - session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; - session->state &= ~LIBSSH2_STATE_EXCHANGING_KEYS; + key_state->state = ssh2_NB_state_idle; + session->state &= ~SSH2_STATE_INITIAL_KEX; + session->state &= ~SSH2_STATE_KEX_ACTIVE; + session->state &= ~SSH2_STATE_EXCHANGING_KEYS; return -1; } if(session->remote.kexinit) { - LIBSSH2_FREE(session, session->remote.kexinit); + SSH2_FREE(session, session->remote.kexinit); } session->remote.kexinit = key_state->data; session->remote.kexinit_len = key_state->data_len; @@ -4306,44 +4235,43 @@ int _libssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange, session->remote.kexinit_len)) rc = LIBSSH2_ERROR_KEX_FAILURE; - key_state->state = libssh2_NB_state_sent2; + key_state->state = ssh2_NB_state_sent2; } } else { - key_state->state = libssh2_NB_state_sent2; + key_state->state = ssh2_NB_state_sent2; } if(rc == 0 && session->kex && session->kex->exchange_keys) { - if(key_state->state == libssh2_NB_state_sent2) { + if(key_state->state == ssh2_NB_state_sent2) { retcode = session->kex->exchange_keys(session, &key_state->key_state_low); if(retcode == LIBSSH2_ERROR_EAGAIN) { - session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; + session->state &= ~SSH2_STATE_KEX_ACTIVE; return retcode; } else if(retcode) { - rc = _libssh2_error(session, - LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE, - "Unrecoverable error exchanging keys"); + rc = ssh2_err(session, LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE, + "Unrecoverable error exchanging keys"); } } } /* Done with kexinit buffers */ if(session->local.kexinit) { - LIBSSH2_FREE(session, session->local.kexinit); + SSH2_FREE(session, session->local.kexinit); session->local.kexinit = NULL; } if(session->remote.kexinit) { - LIBSSH2_FREE(session, session->remote.kexinit); + SSH2_FREE(session, session->remote.kexinit); session->remote.kexinit = NULL; } - session->state &= ~LIBSSH2_STATE_INITIAL_KEX; - session->state &= ~LIBSSH2_STATE_KEX_ACTIVE; - session->state &= ~LIBSSH2_STATE_EXCHANGING_KEYS; + session->state &= ~SSH2_STATE_INITIAL_KEX; + session->state &= ~SSH2_STATE_KEX_ACTIVE; + session->state &= ~SSH2_STATE_EXCHANGING_KEYS; - key_state->state = libssh2_NB_state_idle; + key_state->state = ssh2_NB_state_idle; return rc; } @@ -4368,37 +4296,37 @@ int libssh2_session_method_pref(LIBSSH2_SESSION *session, int method_type, case LIBSSH2_METHOD_HOSTKEY: prefvar = &session->hostkey_prefs; - mlist = (const struct common_method **)libssh2_hostkey_methods(); + mlist = (const struct common_method **)ssh2_hostkey_methods(); break; case LIBSSH2_METHOD_CRYPT_CS: prefvar = &session->local.crypt_prefs; - mlist = (const struct common_method **)libssh2_crypt_methods(); + mlist = (const struct common_method **)ssh2_crypt_methods(); break; case LIBSSH2_METHOD_CRYPT_SC: prefvar = &session->remote.crypt_prefs; - mlist = (const struct common_method **)libssh2_crypt_methods(); + mlist = (const struct common_method **)ssh2_crypt_methods(); break; case LIBSSH2_METHOD_MAC_CS: prefvar = &session->local.mac_prefs; - mlist = (const struct common_method **)_libssh2_mac_methods(); + mlist = (const struct common_method **)ssh2_mac_methods(); break; case LIBSSH2_METHOD_MAC_SC: prefvar = &session->remote.mac_prefs; - mlist = (const struct common_method **)_libssh2_mac_methods(); + mlist = (const struct common_method **)ssh2_mac_methods(); break; case LIBSSH2_METHOD_COMP_CS: prefvar = &session->local.comp_prefs; - mlist = (const struct common_method **)_libssh2_comp_methods(session); + mlist = (const struct common_method **)ssh2_comp_methods(session); break; case LIBSSH2_METHOD_COMP_SC: prefvar = &session->remote.comp_prefs; - mlist = (const struct common_method **)_libssh2_comp_methods(session); + mlist = (const struct common_method **)ssh2_comp_methods(session); break; case LIBSSH2_METHOD_LANG_CS: @@ -4417,17 +4345,17 @@ int libssh2_session_method_pref(LIBSSH2_SESSION *session, int method_type, break; default: - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Invalid parameter specified for method_type"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Invalid parameter specified for method_type"); } - s = newprefs = LIBSSH2_ALLOC(session, prefs_len + 1); + s = newprefs = SSH2_ALLOC(session, prefs_len + 1); if(!newprefs) { if(tmpprefs) { - LIBSSH2_FREE(session, tmpprefs); + SSH2_FREE(session, tmpprefs); } - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Error allocated space for method preferences"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Error allocated space for method preferences"); } memcpy(s, prefs, prefs_len + 1); @@ -4455,14 +4383,13 @@ int libssh2_session_method_pref(LIBSSH2_SESSION *session, int method_type, } if(tmpprefs) { - LIBSSH2_FREE(session, tmpprefs); + SSH2_FREE(session, tmpprefs); } if(!*newprefs) { - LIBSSH2_FREE(session, newprefs); - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "The requested method(s) are not currently " - "supported"); + SSH2_FREE(session, newprefs); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "The requested method(s) are not currently supported"); } /* add method kex extension to the start of the user list */ @@ -4471,23 +4398,23 @@ int libssh2_session_method_pref(LIBSSH2_SESSION *session, int method_type, "ext-info-c,kex-strict-c-v00@openssh.com,"; size_t kex_extensions_len = strlen(kex_extensions); size_t tmp_len = kex_extensions_len + strlen(newprefs); - tmpprefs = LIBSSH2_ALLOC(session, tmp_len + 1); + tmpprefs = SSH2_ALLOC(session, tmp_len + 1); if(!tmpprefs) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Error allocated space for kex method" - " preferences"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Error allocated space for kex method" + " preferences"); } memcpy(tmpprefs, kex_extensions, kex_extensions_len); memcpy(tmpprefs + kex_extensions_len, newprefs, strlen(newprefs)); tmpprefs[tmp_len] = '\0'; - LIBSSH2_FREE(session, newprefs); + SSH2_FREE(session, newprefs); newprefs = tmpprefs; } if(*prefvar) { - LIBSSH2_FREE(session, *prefvar); + SSH2_FREE(session, *prefvar); } *prefvar = newprefs; @@ -4510,8 +4437,8 @@ int libssh2_session_supported_algs(LIBSSH2_SESSION *session, /* to prevent coredumps due to dereferencing of NULL */ if(!algs) - return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE, - "algs must not be NULL"); + return ssh2_err(session, LIBSSH2_ERROR_BAD_USE, + "algs must not be NULL"); switch(method_type) { case LIBSSH2_METHOD_KEX: @@ -4519,22 +4446,22 @@ int libssh2_session_supported_algs(LIBSSH2_SESSION *session, break; case LIBSSH2_METHOD_HOSTKEY: - mlist = (const struct common_method **)libssh2_hostkey_methods(); + mlist = (const struct common_method **)ssh2_hostkey_methods(); break; case LIBSSH2_METHOD_CRYPT_CS: case LIBSSH2_METHOD_CRYPT_SC: - mlist = (const struct common_method **)libssh2_crypt_methods(); + mlist = (const struct common_method **)ssh2_crypt_methods(); break; case LIBSSH2_METHOD_MAC_CS: case LIBSSH2_METHOD_MAC_SC: - mlist = (const struct common_method **)_libssh2_mac_methods(); + mlist = (const struct common_method **)ssh2_mac_methods(); break; case LIBSSH2_METHOD_COMP_CS: case LIBSSH2_METHOD_COMP_SC: - mlist = (const struct common_method **)_libssh2_comp_methods(session); + mlist = (const struct common_method **)ssh2_comp_methods(session); break; case LIBSSH2_METHOD_SIGN_ALGO: @@ -4543,14 +4470,13 @@ int libssh2_session_supported_algs(LIBSSH2_SESSION *session, break; default: - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unknown method type"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unknown method type"); } /* switch */ /* weird situation */ if(!mlist) - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "No algorithm found"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, "No algorithm found"); /* mlist is looped through twice. The first time to find the number od @@ -4574,14 +4500,13 @@ int libssh2_session_supported_algs(LIBSSH2_SESSION *session, /* weird situation, no algorithm found */ if(ialg == 0) - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "No algorithm found"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, "No algorithm found"); /* allocate buffer */ - *algs = (const char **)LIBSSH2_ALLOC(session, ialg * sizeof(const char *)); + *algs = (const char **)SSH2_ALLOC(session, ialg * sizeof(const char *)); if(!*algs) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Memory allocation failed"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Memory allocation failed"); } /* Past this point *algs must be deallocated in case of an error! */ @@ -4600,11 +4525,10 @@ int libssh2_session_supported_algs(LIBSSH2_SESSION *session, /* correct number of pointers copied? (test the code above) */ if(j != ialg) { /* deallocate buffer */ - LIBSSH2_FREE(session, (void *)*algs); + SSH2_FREE(session, (void *)*algs); *algs = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE, - "Internal error"); + return ssh2_err(session, LIBSSH2_ERROR_BAD_USE, "Internal error"); } return ialg; diff --git a/src/knownhost.c b/src/knownhost.c index 7aa0e8b0..026c25a8 100644 --- a/src/knownhost.c +++ b/src/knownhost.c @@ -70,16 +70,16 @@ static void free_host(LIBSSH2_SESSION *session, struct known_host *entry) { if(entry) { if(entry->comment) - LIBSSH2_FREE(session, entry->comment); + SSH2_FREE(session, entry->comment); if(entry->key_type_name) - LIBSSH2_FREE(session, entry->key_type_name); + SSH2_FREE(session, entry->key_type_name); if(entry->key) - LIBSSH2_FREE(session, entry->key); + SSH2_FREE(session, entry->key); if(entry->salt) - LIBSSH2_FREE(session, entry->salt); + SSH2_FREE(session, entry->salt); if(entry->name) - LIBSSH2_FREE(session, entry->name); - LIBSSH2_FREE(session, entry); + SSH2_FREE(session, entry->name); + SSH2_FREE(session, entry); } } @@ -90,18 +90,17 @@ LIBSSH2_API LIBSSH2_KNOWNHOSTS *libssh2_knownhost_init(LIBSSH2_SESSION *session) { LIBSSH2_KNOWNHOSTS *knh = - LIBSSH2_ALLOC(session, sizeof(struct _LIBSSH2_KNOWNHOSTS)); + SSH2_ALLOC(session, sizeof(struct _LIBSSH2_KNOWNHOSTS)); if(!knh) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for known-hosts " - "collection"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for known-hosts collection"); return NULL; } knh->session = session; - _libssh2_list_init(&knh->head); + ssh2_list_init(&knh->head); return knh; } @@ -139,44 +138,41 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, /* make sure we have a key type set */ if(!(typemask & LIBSSH2_KNOWNHOST_KEY_MASK)) - return _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, - "No key type set"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_INVAL, + "No key type set"); - entry = LIBSSH2_CALLOC(hosts->session, sizeof(struct known_host)); + entry = SSH2_CALLOC(hosts->session, sizeof(struct known_host)); if(!entry) - return _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for known host " - "entry"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for known host entry"); entry->typemask = typemask; switch(entry->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) { case LIBSSH2_KNOWNHOST_TYPE_PLAIN: case LIBSSH2_KNOWNHOST_TYPE_CUSTOM: - entry->name = LIBSSH2_ALLOC(hosts->session, hostlen + 1); + entry->name = SSH2_ALLOC(hosts->session, hostlen + 1); if(!entry->name) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for hostname"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for hostname"); goto error; } memcpy(entry->name, host, hostlen + 1); entry->name_len = hostlen; break; case LIBSSH2_KNOWNHOST_TYPE_SHA1: - rc = _libssh2_base64_decode(hosts->session, &ptr, &ptrlen, - host, hostlen); + rc = ssh2_base64_decode(hosts->session, &ptr, &ptrlen, host, hostlen); if(rc) goto error; if(!ptr || ptrlen == 0) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, - "Base64 decoded value is invalid"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_INVAL, + "Base64 decoded value is invalid"); goto error; } if(!salt) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, - "Salt is NULL"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_INVAL, "Salt is NULL"); goto error; } @@ -185,14 +181,14 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, ptr = NULL; ptrlen = 0; - rc = _libssh2_base64_decode(hosts->session, &ptr, &ptrlen, - salt, strlen(salt)); + rc = ssh2_base64_decode(hosts->session, &ptr, &ptrlen, salt, + strlen(salt)); if(rc) goto error; if(!ptr || ptrlen == 0) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, - "Base64 decoded value is invalid"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_INVAL, + "Base64 decoded value is invalid"); goto error; } @@ -200,8 +196,8 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, entry->salt_len = ptrlen; break; default: - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unknown hostname type"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unknown hostname type"); goto error; } @@ -209,10 +205,10 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, /* the provided key is base64 encoded already */ if(!keylen) keylen = strlen(key); - entry->key = LIBSSH2_ALLOC(hosts->session, keylen + 1); + entry->key = SSH2_ALLOC(hosts->session, keylen + 1); if(!entry->key) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for key"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for key"); goto error; } memcpy(entry->key, key, keylen + 1); @@ -220,12 +216,10 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, } else { /* key is raw, we base64 encode it and store it as such */ - size_t nlen = _libssh2_base64_encode(hosts->session, key, keylen, - &ptr); + size_t nlen = ssh2_base64_encode(hosts->session, key, keylen, &ptr); if(!nlen) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "base64-encoded key"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for base64-encoded key"); goto error; } @@ -234,10 +228,10 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, if(key_type_name && ((typemask & LIBSSH2_KNOWNHOST_KEY_MASK) == LIBSSH2_KNOWNHOST_KEY_UNKNOWN)) { - entry->key_type_name = LIBSSH2_ALLOC(hosts->session, key_type_len + 1); + entry->key_type_name = SSH2_ALLOC(hosts->session, key_type_len + 1); if(!entry->key_type_name) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for key type"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for key type"); goto error; } memcpy(entry->key_type_name, key_type_name, key_type_len); @@ -246,10 +240,10 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, } if(comment) { - entry->comment = LIBSSH2_ALLOC(hosts->session, commentlen + 1); + entry->comment = SSH2_ALLOC(hosts->session, commentlen + 1); if(!entry->comment) { - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for comment"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for comment"); goto error; } memcpy(entry->comment, comment, commentlen + 1); @@ -261,7 +255,7 @@ static int knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, } /* add this new host to the big list of known hosts */ - _libssh2_list_add(&hosts->head, &entry->node); + ssh2_list_add(&hosts->head, &entry->node); if(store) *store = knownhost_to_external(entry); @@ -381,8 +375,8 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts, if(port >= 0) { int len = snprintf(hostbuff, sizeof(hostbuff), "[%s]:%d", hostp, port); if(len < 0 || len >= (int)sizeof(hostbuff)) { - _libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Known-host write buffer too small"); + ssh2_err(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Known-host write buffer too small"); return LIBSSH2_KNOWNHOST_CHECK_FAILURE; } host = hostbuff; @@ -395,12 +389,11 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts, if(!(typemask & LIBSSH2_KNOWNHOST_KEYENC_BASE64)) { /* we got a raw key input, convert it to base64 for the checks below */ - size_t nlen = _libssh2_base64_encode(hosts->session, key, keylen, - &keyalloc); + size_t nlen = ssh2_base64_encode(hosts->session, key, keylen, + &keyalloc); if(!nlen) { - _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for base64-encoded " - "key"); + ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for base64-encoded key"); return LIBSSH2_KNOWNHOST_CHECK_FAILURE; } @@ -409,7 +402,7 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts, } do { - node = _libssh2_list_first(&hosts->head); + node = ssh2_list_first(&hosts->head); while(node) { switch(node->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) { case LIBSSH2_KNOWNHOST_TYPE_PLAIN: @@ -427,8 +420,8 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts, stored hash. */ unsigned char hash[SHA_DIGEST_LENGTH]; - libssh2_hmac_ctx ctx; - if(!_libssh2_hmac_ctx_init(&ctx)) + ssh2_hmac_ctx ctx; + if(!ssh2_hmac_ctx_init(&ctx)) break; if(SHA_DIGEST_LENGTH != node->name_len) { @@ -436,15 +429,14 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts, we cannot match it */ break; } - if(!_libssh2_hmac_sha1_init(&ctx, - node->salt, node->salt_len)) + if(!ssh2_hmac_sha1_init(&ctx, node->salt, node->salt_len)) break; - if(!_libssh2_hmac_update(&ctx, host, strlen(host)) || - !_libssh2_hmac_final(&ctx, hash)) { - _libssh2_hmac_cleanup(&ctx); + if(!ssh2_hmac_update(&ctx, host, strlen(host)) || + !ssh2_hmac_final(&ctx, hash)) { + ssh2_hmac_cleanup(&ctx); break; } - _libssh2_hmac_cleanup(&ctx); + ssh2_hmac_cleanup(&ctx); if(!memcmp(hash, node->name, SHA_DIGEST_LENGTH)) /* this is a node we are interested in */ @@ -485,7 +477,7 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts, } match = 0; /* do not count this as a match anymore */ } - node = _libssh2_list_next(&node->node); + node = ssh2_list_next(&node->node); } host = hostp; } while(!match && --numcheck); @@ -498,7 +490,7 @@ static int knownhost_check(LIBSSH2_KNOWNHOSTS *hosts, } if(keyalloc) - LIBSSH2_FREE(hosts->session, keyalloc); + SSH2_FREE(hosts->session, keyalloc); return rc; } @@ -570,14 +562,14 @@ int libssh2_knownhost_del(LIBSSH2_KNOWNHOSTS *hosts, /* check that this was retrieved the right way or get out */ if(!entry || (entry->magic != KNOWNHOST_MAGIC)) - return _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, - "Invalid host information"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_INVAL, + "Invalid host information"); /* get the internal node pointer */ node = entry->node; /* unlink from the list of all hosts */ - _libssh2_list_remove(&node->node); + ssh2_list_remove(&node->node); /* clear the struct now since the memory in which it is allocated is about to be freed! */ @@ -598,11 +590,11 @@ void libssh2_knownhost_free(LIBSSH2_KNOWNHOSTS *hosts) struct known_host *node; struct known_host *next; - for(node = _libssh2_list_first(&hosts->head); node; node = next) { - next = _libssh2_list_next(&node->node); + for(node = ssh2_list_first(&hosts->head); node; node = next) { + next = ssh2_list_next(&node->node); free_host(hosts->session, node); } - LIBSSH2_FREE(hosts->session, hosts); + SSH2_FREE(hosts->session, hosts); } /* old style plain text: [name]([,][name])* @@ -621,10 +613,8 @@ static int oldstyle_hostline(LIBSSH2_KNOWNHOSTS *hosts, const char *name = host + hostlen; if(hostlen < 1) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Failed to parse known_hosts line " - "(no hostnames)"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Failed to parse known_hosts line (no hostnames)"); while(name > host) { --name; @@ -638,10 +628,10 @@ static int oldstyle_hostline(LIBSSH2_KNOWNHOSTS *hosts, /* make sure we do not overflow the buffer */ if(namelen >= sizeof(hostbuf) - 1) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Failed to parse known_hosts line " - "(unexpected length)"); + return ssh2_err(hosts->session, + LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Failed to parse known_hosts line " + "(unexpected length)"); /* copy hostname to the temp buffer and null-terminate */ memcpy(hostbuf, name, namelen); @@ -688,10 +678,9 @@ static int hashed_hostline(LIBSSH2_KNOWNHOSTS *hosts, const char *hash = NULL; size_t saltlen = p - salt; if(saltlen >= (sizeof(saltbuf) - 1)) /* weird length */ - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Failed to parse known_hosts line " - "(unexpectedly long salt)"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Failed to parse known_hosts line " + "(unexpectedly long salt)"); memcpy(saltbuf, salt, saltlen); saltbuf[saltlen] = 0; /* null-terminate */ @@ -705,10 +694,9 @@ static int hashed_hostline(LIBSSH2_KNOWNHOSTS *hosts, /* check that the lengths seem sensible */ if(hostlen >= sizeof(hostbuf) - 1) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Failed to parse known_hosts line " - "(unexpected length)"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Failed to parse known_hosts line " + "(unexpected length)"); memcpy(hostbuf, host, hostlen); hostbuf[hostlen] = 0; @@ -744,10 +732,8 @@ static int hostline(LIBSSH2_KNOWNHOSTS *hosts, /* make some checks that the lengths seem sensible */ if(keylen < 20) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Failed to parse known_hosts line " - "(key too short)"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Failed to parse known_hosts line (key too short)"); switch(key[0]) { case '0': @@ -883,10 +869,8 @@ int libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts, int rc; if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unsupported type of known-host information " - "store"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unsupported type of known-host information store"); cp = line; @@ -918,9 +902,8 @@ int libssh2_knownhost_readline(LIBSSH2_KNOWNHOSTS *hosts, } if(!*cp || !len) /* illegal line */ - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Failed to parse known_hosts line"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Failed to parse known_hosts line"); keyp = cp; /* the key starts here */ keylen = len; @@ -957,17 +940,15 @@ int libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts, char buf[4092]; if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unsupported type of known-host information " - "store"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unsupported type of known-host information store"); file = fopen(filename, FOPEN_READTEXT); if(file) { while(fgets(buf, sizeof(buf), file)) { if(libssh2_knownhost_readline(hosts, buf, strlen(buf), type)) { - num = _libssh2_error(hosts->session, LIBSSH2_ERROR_KNOWN_HOSTS, - "Failed to parse known hosts file"); + num = ssh2_err(hosts->session, LIBSSH2_ERROR_KNOWN_HOSTS, + "Failed to parse known hosts file"); break; } num++; @@ -975,8 +956,8 @@ int libssh2_knownhost_readfile(LIBSSH2_KNOWNHOSTS *hosts, fclose(file); } else - return _libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, - "Failed to open file"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_FILE, + "Failed to open file"); return num; } @@ -1002,10 +983,8 @@ static int knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, /* we only support this single file type for now, bail out on all other attempts */ if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unsupported type of known-host information " - "store"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unsupported type of known-host information store"); switch(node->typemask & LIBSSH2_KNOWNHOST_KEY_MASK) { case LIBSSH2_KNOWNHOST_KEY_RSA1: @@ -1045,11 +1024,10 @@ static int knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, break; } /* otherwise fallback to default and error */ - LIBSSH2_FALLTHROUGH(); + SSH2_FALLTHROUGH(); default: - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unsupported type of known-host entry"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unsupported type of known-host entry"); } /* When putting together the host line there are three aspects to consider: @@ -1086,21 +1064,21 @@ static int knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, char *saltalloc; size_t salt_base64_len; - name_base64_len = _libssh2_base64_encode(hosts->session, node->name, - node->name_len, &namealloc); + name_base64_len = ssh2_base64_encode(hosts->session, node->name, + node->name_len, &namealloc); if(!name_base64_len) - return _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "base64-encoded hostname"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "base64-encoded hostname"); - salt_base64_len = _libssh2_base64_encode(hosts->session, - node->salt, node->salt_len, - &saltalloc); + salt_base64_len = ssh2_base64_encode(hosts->session, + node->salt, node->salt_len, + &saltalloc); if(!salt_base64_len) { - LIBSSH2_FREE(hosts->session, namealloc); - return _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "base64-encoded salt"); + SSH2_FREE(hosts->session, namealloc); + return ssh2_err(hosts->session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "base64-encoded salt"); } required_size += salt_base64_len + name_base64_len + 7; @@ -1121,8 +1099,8 @@ static int knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, node->key); } - LIBSSH2_FREE(hosts->session, namealloc); - LIBSSH2_FREE(hosts->session, saltalloc); + SSH2_FREE(hosts->session, namealloc); + SSH2_FREE(hosts->session, saltalloc); } else { required_size += node->name_len + 3; @@ -1149,8 +1127,8 @@ static int knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, if(required_size <= buflen) return LIBSSH2_ERROR_NONE; else - return _libssh2_error(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Known-host write buffer too small"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Known-host write buffer too small"); } /* @@ -1169,8 +1147,8 @@ int libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, struct known_host *node; if(known->magic != KNOWNHOST_MAGIC) - return _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, - "Invalid host information"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_INVAL, + "Invalid host information"); node = known->node; @@ -1192,19 +1170,17 @@ int libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts, /* we only support this single file type for now, bail out on all other attempts */ if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) - return _libssh2_error(hosts->session, - LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unsupported type of known-host information " - "store"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unsupported type of known-host information store"); file = fopen(filename, FOPEN_WRITETEXT); if(!file) - return _libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, - "Failed to open file"); + return ssh2_err(hosts->session, LIBSSH2_ERROR_FILE, + "Failed to open file"); - for(node = _libssh2_list_first(&hosts->head); + for(node = ssh2_list_first(&hosts->head); node; - node = _libssh2_list_next(&node->node)) { + node = ssh2_list_next(&node->node)) { size_t wrote = 0; size_t nwrote; rc = knownhost_writeline(hosts, node, buffer, sizeof(buffer), &wrote, @@ -1215,8 +1191,7 @@ int libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts, nwrote = fwrite(buffer, 1, wrote, file); if(nwrote != wrote) { /* failed to write the whole thing, bail out */ - rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, - "Write failed"); + rc = ssh2_err(hosts->session, LIBSSH2_ERROR_FILE, "Write failed"); break; } } @@ -1245,10 +1220,10 @@ int libssh2_knownhost_get(LIBSSH2_KNOWNHOSTS *hosts, struct known_host *prev_node = prev->node; /* get the next node in the list */ - node = _libssh2_list_next(&prev_node->node); + node = ssh2_list_next(&prev_node->node); } else - node = _libssh2_list_first(&hosts->head); + node = ssh2_list_first(&hosts->head); if(!node) /* no (more) node */ diff --git a/src/libgcrypt.c b/src/libgcrypt.c index d56e2a09..3e2fe2a5 100644 --- a/src/libgcrypt.c +++ b/src/libgcrypt.c @@ -42,14 +42,14 @@ #ifdef LIBSSH2_LIBGCRYPT -int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx) +int ssh2_hmac_ctx_init(ssh2_hmac_ctx *ctx) { *ctx = NULL; return 1; } #if LIBSSH2_MD5 -int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_md5_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { gcry_error_t err; err = gcry_md_open(ctx, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC); @@ -63,8 +63,7 @@ int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) #endif #if LIBSSH2_HMAC_RIPEMD -int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen) +int ssh2_hmac_ripemd160_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { gcry_error_t err; err = gcry_md_open(ctx, GCRY_MD_RMD160, GCRY_MD_FLAG_HMAC); @@ -77,7 +76,7 @@ int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx, } #endif -int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha1_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { gcry_error_t err; err = gcry_md_open(ctx, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC); @@ -89,7 +88,7 @@ int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) return 1; } -int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha256_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { gcry_error_t err; err = gcry_md_open(ctx, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC); @@ -101,7 +100,7 @@ int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) return 1; } -int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha512_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { gcry_error_t err; err = gcry_md_open(ctx, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC); @@ -113,14 +112,13 @@ int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) return 1; } -int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, - const void *data, size_t datalen) +int ssh2_hmac_update(ssh2_hmac_ctx *ctx, const void *data, size_t datalen) { gcry_md_write(*ctx, data, datalen); return 1; } -int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data) +int ssh2_hmac_final(ssh2_hmac_ctx *ctx, void *data) { unsigned char *res = gcry_md_read(*ctx, 0); @@ -132,21 +130,21 @@ int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data) return 1; } -void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) +void ssh2_hmac_cleanup(ssh2_hmac_ctx *ctx) { gcry_md_close(*ctx); } #if LIBSSH2_RSA -int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, - const unsigned char *edata, unsigned long elen, - const unsigned char *ndata, unsigned long nlen, - const unsigned char *ddata, unsigned long dlen, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *e1data, unsigned long e1len, - const unsigned char *e2data, unsigned long e2len, - const unsigned char *coeffdata, unsigned long coefflen) +int ssh2_rsa_new(ssh2_rsa_ctx **rsa, + const unsigned char *edata, unsigned long elen, + const unsigned char *ndata, unsigned long nlen, + const unsigned char *ddata, unsigned long dlen, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *e1data, unsigned long e1len, + const unsigned char *e2data, unsigned long e2len, + const unsigned char *coeffdata, unsigned long coefflen) { int rc; @@ -173,11 +171,11 @@ int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, return 0; } -int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, - size_t hash_len, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len) +int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsactx, + size_t hash_len, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len) { unsigned char *hash; int ret; @@ -191,15 +189,15 @@ int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, if(hash_len == SHA_DIGEST_LENGTH) { algo = "sha1"; - ret = libssh2_sha1(m, m_len, hash); + ret = ssh2_sha1(m, m_len, hash); } else if(hash_len == SHA256_DIGEST_LENGTH) { algo = "sha256"; - ret = libssh2_sha256(m, m_len, hash); + ret = ssh2_sha256(m, m_len, hash); } else if(hash_len == SHA512_DIGEST_LENGTH) { algo = "sha512"; - ret = libssh2_sha512(m, m_len, hash); + ret = ssh2_sha512(m, m_len, hash); } else { ret = 1; @@ -236,24 +234,24 @@ out: } #if LIBSSH2_RSA_SHA1 -int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx *rsactx, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len) +int ssh2_rsa_sha1_verify(ssh2_rsa_ctx *rsactx, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len) { - return _libssh2_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, sig, sig_len, m, + return ssh2_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, sig, sig_len, m, m_len); } #endif #endif #if LIBSSH2_DSA -int _libssh2_dsa_new(libssh2_dsa_ctx **dsactx, - const unsigned char *p, unsigned long p_len, - const unsigned char *q, unsigned long q_len, - const unsigned char *g, unsigned long g_len, - const unsigned char *y, unsigned long y_len, - const unsigned char *x, unsigned long x_len) +int ssh2_dsa_new(ssh2_dsa_ctx **dsactx, + const unsigned char *p, unsigned long p_len, + const unsigned char *q, unsigned long q_len, + const unsigned char *g, unsigned long g_len, + const unsigned char *y, unsigned long y_len, + const unsigned char *x, unsigned long x_len) { int rc; @@ -278,26 +276,26 @@ int _libssh2_dsa_new(libssh2_dsa_ctx **dsactx, #endif #if LIBSSH2_RSA -int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { (void)rsa; (void)filedata; (void)filedata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unable to extract private key from memory: " - "Method unimplemented in libgcrypt backend"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unable to extract private key from memory: " + "Method unimplemented in libgcrypt backend"); } -int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_rsa_new_private(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { FILE *fp; unsigned char *data, *save_data; @@ -311,11 +309,10 @@ int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, return -1; } - ret = _libssh2_pem_parse(session, - "-----BEGIN RSA PRIVATE KEY-----", - "-----END RSA PRIVATE KEY-----", - passphrase, - fp, &data, &datalen); + ret = ssh2_pem_parse(session, + "-----BEGIN RSA PRIVATE KEY-----", + "-----END RSA PRIVATE KEY-----", + passphrase, fp, &data, &datalen); fclose(fp); if(ret) { return -1; @@ -323,68 +320,68 @@ int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, save_data = data; - if(_libssh2_pem_decode_sequence(&data, &datalen)) { + if(ssh2_pem_decode_sequence(&data, &datalen)) { ret = -1; goto fail; } /* First read Version field (should be 0). */ - ret = _libssh2_pem_decode_integer(&data, &datalen, &n, &nlen); + ret = ssh2_pem_decode_integer(&data, &datalen, &n, &nlen); if(ret || (nlen != 1 && *n != '\0')) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &n, &nlen); + ret = ssh2_pem_decode_integer(&data, &datalen, &n, &nlen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &e, &elen); + ret = ssh2_pem_decode_integer(&data, &datalen, &e, &elen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &d, &dlen); + ret = ssh2_pem_decode_integer(&data, &datalen, &d, &dlen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &p, &plen); + ret = ssh2_pem_decode_integer(&data, &datalen, &p, &plen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &q, &qlen); + ret = ssh2_pem_decode_integer(&data, &datalen, &q, &qlen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &e1, &e1len); + ret = ssh2_pem_decode_integer(&data, &datalen, &e1, &e1len); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &e2, &e2len); + ret = ssh2_pem_decode_integer(&data, &datalen, &e2, &e2len); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &coeff, &coefflen); + ret = ssh2_pem_decode_integer(&data, &datalen, &coeff, &coefflen); if(ret) { ret = -1; goto fail; } - if(_libssh2_rsa_new(rsa, e, elen, n, nlen, d, dlen, p, plen, - q, qlen, e1, e1len, e2, e2len, coeff, coefflen)) { + if(ssh2_rsa_new(rsa, e, elen, n, nlen, d, dlen, p, plen, + q, qlen, e1, e1len, e2, e2len, coeff, coefflen)) { ret = -1; goto fail; } @@ -392,32 +389,32 @@ int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, ret = 0; fail: - LIBSSH2_FREE(session, save_data); + SSH2_FREE(session, save_data); return ret; } #endif #if LIBSSH2_DSA -int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_dsa_new_private_frommemory(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { (void)dsa; (void)filedata; (void)filedata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unable to extract private key from memory: " - "Method unimplemented in libgcrypt backend"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unable to extract private key from memory: " + "Method unimplemented in libgcrypt backend"); } -int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_dsa_new_private(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { FILE *fp; unsigned char *data, *save_data; @@ -431,11 +428,10 @@ int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, return -1; } - ret = _libssh2_pem_parse(session, - "-----BEGIN DSA PRIVATE KEY-----", - "-----END DSA PRIVATE KEY-----", - passphrase, - fp, &data, &datalen); + ret = ssh2_pem_parse(session, + "-----BEGIN DSA PRIVATE KEY-----", + "-----END DSA PRIVATE KEY-----", + passphrase, fp, &data, &datalen); fclose(fp); if(ret) { return -1; @@ -443,43 +439,43 @@ int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, save_data = data; - if(_libssh2_pem_decode_sequence(&data, &datalen)) { + if(ssh2_pem_decode_sequence(&data, &datalen)) { ret = -1; goto fail; } /* First read Version field (should be 0). */ - ret = _libssh2_pem_decode_integer(&data, &datalen, &p, &plen); + ret = ssh2_pem_decode_integer(&data, &datalen, &p, &plen); if(ret || (plen != 1 && *p != '\0')) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &p, &plen); + ret = ssh2_pem_decode_integer(&data, &datalen, &p, &plen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &q, &qlen); + ret = ssh2_pem_decode_integer(&data, &datalen, &q, &qlen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &g, &glen); + ret = ssh2_pem_decode_integer(&data, &datalen, &g, &glen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &y, &ylen); + ret = ssh2_pem_decode_integer(&data, &datalen, &y, &ylen); if(ret) { ret = -1; goto fail; } - ret = _libssh2_pem_decode_integer(&data, &datalen, &x, &xlen); + ret = ssh2_pem_decode_integer(&data, &datalen, &x, &xlen); if(ret) { ret = -1; goto fail; @@ -490,7 +486,7 @@ int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, goto fail; } - if(_libssh2_dsa_new(dsa, p, plen, q, qlen, g, glen, y, ylen, x, xlen)) { + if(ssh2_dsa_new(dsa, p, plen, q, qlen, g, glen, y, ylen, x, xlen)) { ret = -1; goto fail; } @@ -498,18 +494,18 @@ int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, ret = 0; fail: - LIBSSH2_FREE(session, save_data); + SSH2_FREE(session, save_data); return ret; } #endif #if LIBSSH2_RSA -int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +int ssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { const char *algo; gcry_sexp_t s_tmp = NULL; @@ -527,8 +523,8 @@ int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, else if(hash_len == SHA512_DIGEST_LENGTH) algo = "sha512"; else { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unsupported hash digest length"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unsupported hash digest length"); return -1; } @@ -555,7 +551,7 @@ int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, --size; } - out_sig = LIBSSH2_ALLOC(session, size); + out_sig = SSH2_ALLOC(session, size); if(!out_sig) goto out; memcpy(out_sig, s, size); @@ -574,23 +570,23 @@ out: } #if LIBSSH2_RSA_SHA1 -int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +int ssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { - return _libssh2_rsa_sha2_sign(session, rsactx, hash, hash_len, - signature, signature_len); + return ssh2_rsa_sha2_sign(session, rsactx, hash, hash_len, + signature, signature_len); } #endif #endif #if LIBSSH2_DSA -int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx *dsactx, - const unsigned char *hash, - size_t hash_len, unsigned char *sig) +int ssh2_dsa_sha1_sign(ssh2_dsa_ctx *dsactx, + const unsigned char *hash, + size_t hash_len, unsigned char *sig) { unsigned char zhash[SHA_DIGEST_LENGTH + 1]; gcry_sexp_t sig_sexp; @@ -677,15 +673,15 @@ out: return ret; } -int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, - const unsigned char *sig, - const unsigned char *m, size_t m_len) +int ssh2_dsa_sha1_verify(ssh2_dsa_ctx *dsactx, + const unsigned char *sig, + const unsigned char *m, size_t m_len) { unsigned char hash[SHA_DIGEST_LENGTH + 1]; gcry_sexp_t s_sig, s_hash; int rc = -1; - if(libssh2_sha1(m, m_len, hash + 1)) { + if(ssh2_sha1(m, m_len, hash + 1)) { return -1; } hash[0] = 0; @@ -709,13 +705,12 @@ int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, } #endif -int _libssh2_cipher_init(libssh2_cipher_ctx *h, - LIBSSH2_CIPHER_T(algo), - unsigned char *iv, unsigned char *secret, int encrypt) +int ssh2_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo), + unsigned char *iv, unsigned char *secret, int encrypt) { int ret; - int cipher = _libssh2_gcry_cipher(algo); - int mode = _libssh2_gcry_mode(algo); + int cipher = ssh2_gcry_cipher(algo); + int mode = ssh2_gcry_mode(algo); size_t keylen = gcry_cipher_get_algo_keylen(cipher); (void)encrypt; @@ -746,10 +741,10 @@ int _libssh2_cipher_init(libssh2_cipher_ctx *h, return 0; } -int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, - LIBSSH2_CIPHER_T(algo), - int encrypt, unsigned char *block, size_t blocksize, - int firstlast) +int ssh2_cipher_crypt(ssh2_cipher_ctx *ctx, + SSH2_CIPHER_T(algo), + int encrypt, unsigned char *block, size_t blocksize, + int firstlast) { int ret; @@ -765,14 +760,14 @@ int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, return ret; } -int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { (void)method; (void)method_len; @@ -782,18 +777,18 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, (void)privatekeydata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unable to extract public key from private key in " - "memory: Method unimplemented in libgcrypt backend"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unable to extract public key from private key in " + "memory: Method unimplemented in libgcrypt backend"); } -int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekey, - const char *passphrase) +int ssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekey, + const char *passphrase) { (void)method; (void)method_len; @@ -802,24 +797,24 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, (void)privatekey; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key from private key " - "file: Method unimplemented in libgcrypt backend"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key from private key " + "file: Method unimplemented in libgcrypt backend"); } -int _libssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - int *algorithm, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + int *algorithm, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { (void)method; (void)method_len; @@ -834,23 +829,18 @@ int _libssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, (void)privatekeydata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public SK key from private key " - "file: Method unimplemented in libgcrypt backend"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public SK key from private key " + "file: Method unimplemented in libgcrypt backend"); } -void _libssh2_init_aes_ctr(void) -{ - /* no implementation */ -} - -void _libssh2_dh_init(libssh2_dh_ctx *dhctx) +void ssh2_lgcr_dh_init(ssh2_dh_ctx *dhctx) { *dhctx = gcry_mpi_new(0); /* Random from client */ } -int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, int group_order) +int ssh2_lgcr_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, int group_order) { /* Generate x and e */ gcry_mpi_randomize(*dhctx, group_order * 8 - 1, GCRY_VERY_STRONG_RANDOM); @@ -858,15 +848,15 @@ int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, return 0; } -int _libssh2_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p) +int ssh2_lgcr_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p) { /* Compute the shared secret */ gcry_mpi_powm(secret, f, *dhctx, p); return 0; } -void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx) +void ssh2_lgcr_dh_dtor(ssh2_dh_ctx *dhctx) { gcry_mpi_release(*dhctx); *dhctx = NULL; @@ -875,9 +865,9 @@ void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx) /* * Return supported key hash algo upgrades, see crypto.h */ -const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session, - unsigned char *key_method, - size_t key_method_len) +const char *ssh2_supported_key_sign_algs(LIBSSH2_SESSION *session, + unsigned char *key_method, + size_t key_method_len) { (void)session; diff --git a/src/libgcrypt.h b/src/libgcrypt.h index 8b7301b6..a802dd28 100644 --- a/src/libgcrypt.h +++ b/src/libgcrypt.h @@ -41,7 +41,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -#define LIBSSH2_CRYPTO_ENGINE libssh2_gcrypt +#define SSH2_CRYPTO_ENGINE libssh2_gcrypt #include @@ -79,157 +79,153 @@ #define EC_MAX_POINT_LEN ((528 * 2 / 8) + 1) -#define _libssh2_random(buf, len) \ +#define ssh2_random(buf, len) \ (gcry_randomize(buf, len, GCRY_STRONG_RANDOM), 0) -#define libssh2_prepare_iovec(vec, len) /* Empty. */ +#define ssh2_prepare_iovec(vec, len) /* Empty. */ -#define libssh2_sha1_ctx gcry_md_hd_t +#define ssh2_sha1_ctx gcry_md_hd_t /* returns 0 in case of failure */ -#define libssh2_sha1_init(ctx) \ +#define ssh2_sha1_init(ctx) \ (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA1, 0)) -#define libssh2_sha1_update(ctx, data, len) \ +#define ssh2_sha1_update(ctx, data, len) \ (gcry_md_write(ctx, data, len), 1) -#define libssh2_sha1_final(ctx, out) \ +#define ssh2_sha1_final(ctx, out) \ (memcpy(out, gcry_md_read(ctx, 0), SHA_DIGEST_LENGTH), \ gcry_md_close(ctx), 1) -#define libssh2_sha1(message, len, out) \ +#define ssh2_sha1(message, len, out) \ (gcry_md_hash_buffer(GCRY_MD_SHA1, out, message, len), 0) -#define libssh2_sha256_ctx gcry_md_hd_t -#define libssh2_sha256_init(ctx) \ +#define ssh2_sha256_ctx gcry_md_hd_t +#define ssh2_sha256_init(ctx) \ (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA256, 0)) -#define libssh2_sha256_update(ctx, data, len) \ +#define ssh2_sha256_update(ctx, data, len) \ (gcry_md_write(ctx, data, len), 1) -#define libssh2_sha256_final(ctx, out) \ +#define ssh2_sha256_final(ctx, out) \ (memcpy(out, gcry_md_read(ctx, 0), SHA256_DIGEST_LENGTH), \ gcry_md_close(ctx), 1) -#define libssh2_sha256(message, len, out) \ +#define ssh2_sha256(message, len, out) \ (gcry_md_hash_buffer(GCRY_MD_SHA256, out, message, len), 0) -#define libssh2_sha384_ctx gcry_md_hd_t -#define libssh2_sha384_init(ctx) \ +#define ssh2_sha384_ctx gcry_md_hd_t +#define ssh2_sha384_init(ctx) \ (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA384, 0)) -#define libssh2_sha384_update(ctx, data, len) \ +#define ssh2_sha384_update(ctx, data, len) \ (gcry_md_write(ctx, data, len), 1) -#define libssh2_sha384_final(ctx, out) \ +#define ssh2_sha384_final(ctx, out) \ (memcpy(out, gcry_md_read(ctx, 0), SHA384_DIGEST_LENGTH), \ gcry_md_close(ctx), 1) -#define libssh2_sha384(message, len, out) \ +#define ssh2_sha384(message, len, out) \ (gcry_md_hash_buffer(GCRY_MD_SHA384, out, message, len), 0) -#define libssh2_sha512_ctx gcry_md_hd_t -#define libssh2_sha512_init(ctx) \ +#define ssh2_sha512_ctx gcry_md_hd_t +#define ssh2_sha512_init(ctx) \ (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_SHA512, 0)) -#define libssh2_sha512_update(ctx, data, len) \ +#define ssh2_sha512_update(ctx, data, len) \ (gcry_md_write(ctx, data, len), 1) -#define libssh2_sha512_final(ctx, out) \ +#define ssh2_sha512_final(ctx, out) \ (memcpy(out, gcry_md_read(ctx, 0), SHA512_DIGEST_LENGTH), \ gcry_md_close(ctx), 1) -#define libssh2_sha512(message, len, out) \ +#define ssh2_sha512(message, len, out) \ (gcry_md_hash_buffer(GCRY_MD_SHA512, out, message, len), 0) #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM -#define libssh2_md5_ctx gcry_md_hd_t -#define libssh2_md5_init(ctx) \ +#define ssh2_md5_ctx gcry_md_hd_t +#define ssh2_md5_init(ctx) \ (GPG_ERR_NO_ERROR == gcry_md_open(ctx, GCRY_MD_MD5, 0)) -#define libssh2_md5_update(ctx, data, len) \ +#define ssh2_md5_update(ctx, data, len) \ (gcry_md_write(ctx, data, len), 1) -#define libssh2_md5_final(ctx, out) \ +#define ssh2_md5_final(ctx, out) \ (memcpy(out, gcry_md_read(ctx, 0), MD5_DIGEST_LENGTH), \ gcry_md_close(ctx), 1) #endif -#define libssh2_hmac_ctx gcry_md_hd_t +#define ssh2_hmac_ctx gcry_md_hd_t -#define libssh2_crypto_init() gcry_control(GCRYCTL_DISABLE_SECMEM) -#define libssh2_crypto_exit() +#define ssh2_crypto_init() gcry_control(GCRYCTL_DISABLE_SECMEM) +#define ssh2_crypto_exit() -#define libssh2_rsa_ctx struct gcry_sexp +#define ssh2_rsa_ctx struct gcry_sexp +#define ssh2_rsa_free(rsactx) gcry_sexp_release(rsactx) -#define _libssh2_rsa_free(rsactx) gcry_sexp_release(rsactx) - -#define libssh2_dsa_ctx struct gcry_sexp - -#define _libssh2_dsa_free(dsactx) gcry_sexp_release(dsactx) +#define ssh2_dsa_ctx struct gcry_sexp +#define ssh2_dsa_free(dsactx) gcry_sexp_release(dsactx) #if LIBSSH2_ECDSA #else -#define libssh2_ec_key void +#define ssh2_ec_key void #endif -#define LIBSSH2_CIPHER_T(name) int name -#define libssh2_cipher_ctx gcry_cipher_hd_t +#define SSH2_CIPHER_T(name) int name +#define ssh2_cipher_ctx gcry_cipher_hd_t -#define _libssh2_gcry_ciphermode(c,m) (((c) << 8) | (m)) -#define _libssh2_gcry_cipher(c) ((c) >> 8) -#define _libssh2_gcry_mode(m) ((m) & 0xFF) +#define ssh2_gcry_ciphermode(c,m) (((c) << 8) | (m)) +#define ssh2_gcry_cipher(c) ((c) >> 8) +#define ssh2_gcry_mode(m) ((m) & 0xFF) -#define libssh2_cipher_aes256ctr \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CTR) -#define libssh2_cipher_aes192ctr \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CTR) -#define libssh2_cipher_aes128ctr \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR) -#define libssh2_cipher_aes256 \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC) -#define libssh2_cipher_aes192 \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC) -#define libssh2_cipher_aes128 \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC) -#define libssh2_cipher_blowfish \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC) -#define libssh2_cipher_arcfour \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM) -#define libssh2_cipher_cast5 \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC) -#define libssh2_cipher_3des \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC) -#define libssh2_cipher_chacha20 \ - _libssh2_gcry_ciphermode(GCRY_CIPHER_CHACHA20, GCRY_CIPHER_MODE_STREAM) +#define ssh2_cipher_aes256ctr \ + ssh2_gcry_ciphermode(GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CTR) +#define ssh2_cipher_aes192ctr \ + ssh2_gcry_ciphermode(GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CTR) +#define ssh2_cipher_aes128ctr \ + ssh2_gcry_ciphermode(GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR) +#define ssh2_cipher_aes256 \ + ssh2_gcry_ciphermode(GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC) +#define ssh2_cipher_aes192 \ + ssh2_gcry_ciphermode(GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC) +#define ssh2_cipher_aes128 \ + ssh2_gcry_ciphermode(GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC) +#define ssh2_cipher_blowfish \ + ssh2_gcry_ciphermode(GCRY_CIPHER_BLOWFISH, GCRY_CIPHER_MODE_CBC) +#define ssh2_cipher_arcfour \ + ssh2_gcry_ciphermode(GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM) +#define ssh2_cipher_cast5 \ + ssh2_gcry_ciphermode(GCRY_CIPHER_CAST5, GCRY_CIPHER_MODE_CBC) +#define ssh2_cipher_3des \ + ssh2_gcry_ciphermode(GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC) +#define ssh2_cipher_chacha20 \ + ssh2_gcry_ciphermode(GCRY_CIPHER_CHACHA20, GCRY_CIPHER_MODE_STREAM) -#define _libssh2_cipher_dtor(ctx) gcry_cipher_close(*(ctx)) +#define ssh2_cipher_dtor(ctx) gcry_cipher_close(*(ctx)) -#define libssh2_bn struct gcry_mpi -#define libssh2_bn_ctx int -#define _libssh2_bn_ctx_new() 0 -#define _libssh2_bn_ctx_free(bnctx) ((void)0) -#define _libssh2_bn_init() gcry_mpi_new(0) -#define _libssh2_bn_init_from_bin() NULL /* because gcry_mpi_scan() creates a - new bignum */ -#define _libssh2_bn_set_word(bn, val) gcry_mpi_set_ui(bn, val) -#define _libssh2_bn_from_bin(bn, len, val) \ +#define ssh2_bn struct gcry_mpi +#define ssh2_bn_ctx int +#define ssh2_bn_ctx_new() 0 +#define ssh2_bn_ctx_free(bnctx) ((void)0) +#define ssh2_bn_init() gcry_mpi_new(0) +#define ssh2_bn_init_from_bin() NULL /* because gcry_mpi_scan() creates a + new bignum */ +#define ssh2_bn_set_word(bn, val) gcry_mpi_set_ui(bn, val) +#define ssh2_bn_from_bin(bn, len, val) \ gcry_mpi_scan(&(bn), GCRYMPI_FMT_USG, val, len, NULL) -#define _libssh2_bn_to_bin(bn, val) \ - gcry_mpi_print(GCRYMPI_FMT_USG, val, _libssh2_bn_bytes(bn), NULL, bn) -#define _libssh2_bn_bytes(bn) \ - (gcry_mpi_get_nbits(bn) / 8 + \ - ((gcry_mpi_get_nbits(bn) % 8 == 0) ? 0 : 1)) -#define _libssh2_bn_bits(bn) gcry_mpi_get_nbits(bn) -#define _libssh2_bn_free(bn) gcry_mpi_release(bn) +#define ssh2_bn_to_bin(bn, val) \ + gcry_mpi_print(GCRYMPI_FMT_USG, val, ssh2_bn_bytes(bn), NULL, bn) +#define ssh2_bn_bytes(bn) \ + (gcry_mpi_get_nbits(bn) / 8 + ((gcry_mpi_get_nbits(bn) % 8 == 0) ? 0 : 1)) +#define ssh2_bn_bits(bn) gcry_mpi_get_nbits(bn) +#define ssh2_bn_free(bn) gcry_mpi_release(bn) /* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */ -#define LIBSSH2_DH_GEX_MINGROUP 2048 -#define LIBSSH2_DH_GEX_OPTGROUP 4096 -#define LIBSSH2_DH_GEX_MAXGROUP 8192 +#define SSH2_DH_GEX_MINGROUP 2048 +#define SSH2_DH_GEX_OPTGROUP 4096 +#define SSH2_DH_GEX_MAXGROUP 8192 -#define LIBSSH2_DH_MAX_MODULUS_BITS 16384 +#define SSH2_DH_MAX_MODULUS_BITS 16384 -#define libssh2_dh_ctx struct gcry_mpi * -#define libssh2_dh_init(dhctx) _libssh2_dh_init(dhctx) -#define libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ - _libssh2_dh_key_pair(dhctx, public, g, p, group_order) -#define libssh2_dh_secret(dhctx, secret, f, p, bnctx) \ - _libssh2_dh_secret(dhctx, secret, f, p) -#define libssh2_dh_dtor(dhctx) _libssh2_dh_dtor(dhctx) -void _libssh2_init_aes_ctr(void); -void _libssh2_dh_init(libssh2_dh_ctx *dhctx); -int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, - int group_order); -int _libssh2_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p); -void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx); +#define ssh2_dh_ctx struct gcry_mpi * +#define ssh2_dh_init(dhctx) ssh2_lgcr_dh_init(dhctx) +#define ssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ + ssh2_lgcr_dh_key_pair(dhctx, public, g, p, group_order) +#define ssh2_dh_secret(dhctx, secret, f, p, bnctx) \ + ssh2_lgcr_dh_secret(dhctx, secret, f, p) +#define ssh2_dh_dtor(dhctx) ssh2_lgcr_dh_dtor(dhctx) +void ssh2_lgcr_dh_init(ssh2_dh_ctx *dhctx); +int ssh2_lgcr_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, + int group_order); +int ssh2_lgcr_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p); +void ssh2_lgcr_dh_dtor(ssh2_dh_ctx *dhctx); #endif /* LIBSSH2_LIBGCRYPT_H */ diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index b66e5b40..4247d989 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -124,53 +124,53 @@ #endif #ifdef _WIN64 -#define LIBSSH2_UNCONST(p) ((void *)(libssh2_uint64_t)(const void *)(p)) +#define SSH2_UNCONST(p) ((void *)(libssh2_uint64_t)(const void *)(p)) #elif defined(_MSC_VER) -#define LIBSSH2_UNCONST(p) ((void *)(unsigned int)(const void *)(p)) +#define SSH2_UNCONST(p) ((void *)(unsigned int)(const void *)(p)) #else -#define LIBSSH2_UNCONST(p) ((void *)(uintptr_t)(const void *)(p)) +#define SSH2_UNCONST(p) ((void *)(uintptr_t)(const void *)(p)) #endif #if (defined(__GNUC__) || defined(__clang__)) && \ defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ !defined(LIBSSH2_NO_FMT_CHECKS) #ifdef __MINGW_PRINTF_FORMAT -#define LIBSSH2_PRINTF(fmt, arg) \ +#define SSH2_PRINTF(fmt, arg) \ __attribute__((format(__MINGW_PRINTF_FORMAT, fmt, arg))) #elif !defined(__MINGW32__) -#define LIBSSH2_PRINTF(fmt, arg) \ +#define SSH2_PRINTF(fmt, arg) \ __attribute__((format(printf, fmt, arg))) #endif #endif -#ifndef LIBSSH2_PRINTF -#define LIBSSH2_PRINTF(fmt, arg) +#ifndef SSH2_PRINTF +#define SSH2_PRINTF(fmt, arg) #endif /* Use local implementation when not available */ #ifndef HAVE_SNPRINTF #undef snprintf -#define snprintf _libssh2_snprintf +#define snprintf ssh2_snprintf #define LIBSSH2_SNPRINTF -int _libssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...) - LIBSSH2_PRINTF(3, 4); +int ssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...) + SSH2_PRINTF(3, 4); #endif #ifndef HAVE_GETTIMEOFDAY #define HAVE_GETTIMEOFDAY #undef gettimeofday -#define gettimeofday _libssh2_gettimeofday +#define gettimeofday ssh2_gettimeofday #define LIBSSH2_GETTIMEOFDAY -int _libssh2_gettimeofday(struct timeval *tp, void *tzp); +int ssh2_gettimeofday(struct timeval *tp, void *tzp); #elif defined(HAVE_SYS_TIME_H) #include #endif -#ifndef LIBSSH2_FALLTHROUGH +#ifndef SSH2_FALLTHROUGH #if (defined(__GNUC__) && __GNUC__ >= 7) || \ (defined(__clang__) && __clang_major__ >= 10) -# define LIBSSH2_FALLTHROUGH() __attribute__((fallthrough)) +# define SSH2_FALLTHROUGH() __attribute__((fallthrough)) #else -# define LIBSSH2_FALLTHROUGH() do {} while(0) +# define SSH2_FALLTHROUGH() do {} while(0) #endif #endif @@ -212,8 +212,8 @@ struct iovec { #define UINT_MAX 0xFFFFFFFF #endif -#define LIBSSH2_MAX(x, y) ((x) > (y) ? (x) : (y)) -#define LIBSSH2_MIN(x, y) ((x) < (y) ? (x) : (y)) +#define SSH2_MAX(x, y) ((x) > (y) ? (x) : (y)) +#define SSH2_MIN(x, y) ((x) < (y) ? (x) : (y)) #define MAX_BLOCKSIZE 32 /* MUST fit biggest crypto block size we use/get */ #define MAX_MACSIZE 64 /* MUST fit biggest MAC length we support */ @@ -228,89 +228,90 @@ struct iovec { #define MAX_SSH_PACKET_LEN 35000 #define MAX_SHA_DIGEST_LEN SHA512_DIGEST_LENGTH -#define LIBSSH2_ALLOC(session, count) \ +#define SSH2_ALLOC(session, count) \ session->alloc(count, &(session)->abstract) -#define LIBSSH2_CALLOC(session, count) _libssh2_calloc(session, count) -#define LIBSSH2_REALLOC(session, ptr, count) \ +#define SSH2_CALLOC(session, count) ssh2_calloc(session, count) +#define SSH2_REALLOC(session, ptr, count) \ ((ptr) ? (session)->realloc(ptr, count, &(session)->abstract) : \ (session)->alloc(count, &(session)->abstract)) -#define LIBSSH2_FREE(session, ptr) \ +#define SSH2_FREE(session, ptr) \ session->free(ptr, &(session)->abstract) -#define LIBSSH2_IGNORE(session, data, datalen) \ +#define SSH2_IGNORE(session, data, datalen) \ session->ssh_msg_ignore(session, data, (int)(datalen), \ &(session)->abstract) -#define LIBSSH2_DEBUG(session, always_display, message, message_len, \ - language, language_len) \ +#define SSH2_DEBUG(session, always_display, message, message_len, \ + language, language_len) \ session->ssh_msg_debug(session, always_display, \ message, (int)(message_len), \ language, (int)(language_len), \ &(session)->abstract) -#define LIBSSH2_DISCONNECT(session, reason, message, message_len, \ - language, language_len) \ +#define SSH2_DISCONNECT(session, reason, message, message_len, \ + language, language_len) \ session->ssh_msg_disconnect(session, reason, \ message, (int)(message_len), \ language, (int)(language_len), \ &(session)->abstract) -#define LIBSSH2_MACERROR(session, data, datalen) \ +#define SSH2_MACERROR(session, data, datalen) \ session->macerror(session, data, (int)(datalen), &(session)->abstract) -#define LIBSSH2_X11_OPEN(channel, shost, sport) \ +#define SSH2_X11_OPEN(channel, shost, sport) \ channel->session->x11((channel)->session, channel, \ shost, sport, &(channel)->session->abstract) -#define LIBSSH2_AUTHAGENT(channel) \ +#define SSH2_AUTHAGENT(channel) \ channel->session->authagent((channel)->session, channel, \ &(channel)->session->abstract) -#define LIBSSH2_ADD_IDENTITIES(session, buffer, agentPath) \ +#define SSH2_ADD_IDENTITIES(session, buffer, agentPath) \ session->addLocalIdentities(session, buffer, \ agentPath, &(session)->abstract) -#define LIBSSH2_AUTHAGENT_SIGN(session, blob, blen, \ - data, dlen, sig, sigLen, \ - agentPath) \ +#define SSH2_AUTHAGENT_SIGN(session, blob, blen, \ + data, dlen, sig, sigLen, \ + agentPath) \ session->agentSignCallback(session, blob, blen, \ data, dlen, sig, sigLen, \ agentPath, &(session)->abstract) -#define LIBSSH2_CHANNEL_CLOSE(session, channel) \ +#define SSH2_CHANNEL_CLOSE(session, channel) \ channel->close_cb(session, &(session)->abstract, \ channel, &(channel)->abstract) -#define LIBSSH2_SEND_FD(session, fd, buffer, length, flags) \ +#define SSH2_SEND_FD(session, fd, buffer, length, flags) \ ((session)->send)(fd, buffer, length, flags, &(session)->abstract) -#define LIBSSH2_RECV_FD(session, fd, buffer, length, flags) \ +#define SSH2_RECV_FD(session, fd, buffer, length, flags) \ ((session)->recv)(fd, buffer, length, flags, &(session)->abstract) -#define LIBSSH2_SEND(session, buffer, length, flags) \ - LIBSSH2_SEND_FD(session, (session)->socket_fd, buffer, length, flags) -#define LIBSSH2_RECV(session, buffer, length, flags) \ - LIBSSH2_RECV_FD(session, (session)->socket_fd, buffer, length, flags) +#define SSH2_SEND(session, buffer, length, flags) \ + SSH2_SEND_FD(session, (session)->socket_fd, buffer, length, flags) +#define SSH2_RECV(session, buffer, length, flags) \ + SSH2_RECV_FD(session, (session)->socket_fd, buffer, length, flags) +/* non-blocking states */ typedef enum { - libssh2_NB_state_idle = 0, - libssh2_NB_state_allocated, - libssh2_NB_state_created, - libssh2_NB_state_sent, - libssh2_NB_state_sent1, - libssh2_NB_state_sent2, - libssh2_NB_state_sent3, - libssh2_NB_state_sent4, - libssh2_NB_state_sent5, - libssh2_NB_state_sent6, - libssh2_NB_state_sent7, - libssh2_NB_state_jump1, - libssh2_NB_state_jump2, - libssh2_NB_state_jump3, - libssh2_NB_state_jump4, - libssh2_NB_state_jump5, - libssh2_NB_state_error_closing, - libssh2_NB_state_end, - libssh2_NB_state_jumpauthagent -} libssh2_nonblocking_states; + ssh2_NB_state_idle = 0, + ssh2_NB_state_allocated, + ssh2_NB_state_created, + ssh2_NB_state_sent, + ssh2_NB_state_sent1, + ssh2_NB_state_sent2, + ssh2_NB_state_sent3, + ssh2_NB_state_sent4, + ssh2_NB_state_sent5, + ssh2_NB_state_sent6, + ssh2_NB_state_sent7, + ssh2_NB_state_jump1, + ssh2_NB_state_jump2, + ssh2_NB_state_jump3, + ssh2_NB_state_jump4, + ssh2_NB_state_jump5, + ssh2_NB_state_error_closing, + ssh2_NB_state_end, + ssh2_NB_state_jumpauthagent +} ssh2_NB_states; struct packet_require_state { - libssh2_nonblocking_states state; + ssh2_NB_states state; time_t start; }; @@ -319,7 +320,7 @@ struct packet_requirev_state { }; struct kmdhgGPshakex_state { - libssh2_nonblocking_states state; + ssh2_NB_states state; unsigned char *e_packet; unsigned char *s_packet; unsigned char *tmp; @@ -328,11 +329,11 @@ struct kmdhgGPshakex_state { size_t e_packet_len; size_t s_packet_len; size_t tmp_len; - libssh2_bn_ctx *ctx; - libssh2_dh_ctx x; - libssh2_bn *e; - libssh2_bn *f; - libssh2_bn *k; + ssh2_bn_ctx *ctx; + ssh2_dh_ctx x; + ssh2_bn *e; + ssh2_bn *f; + ssh2_bn *k; unsigned char *f_value; unsigned char *k_value; unsigned char *h_sig; @@ -340,22 +341,22 @@ struct kmdhgGPshakex_state { size_t k_value_len; size_t h_sig_len; struct packet_require_state req_state; - libssh2_nonblocking_states burn_state; + ssh2_NB_states burn_state; }; struct key_exchange_state_low { - libssh2_nonblocking_states state; + ssh2_NB_states state; struct packet_require_state req_state; struct kmdhgGPshakex_state exchange_state; - libssh2_bn *p; /* SSH2 defined value (p_value) */ - libssh2_bn *g; /* SSH2 defined value (2) */ + ssh2_bn *p; /* SSH2 defined value (p_value) */ + ssh2_bn *g; /* SSH2 defined value (2) */ /* Request must fit mlkem1024nistp384 keys + 5 bytes overhead */ - unsigned char request[LIBSSH2_MLKEM_1024_PUBLIC_KEY_LEN + - LIBSSH2_EC_P384_PUBLIC_KEY_LEN + 5]; + unsigned char request[SSH2_MLKEM_1024_PUBLIC_KEY_LEN + + SSH2_EC_P384_PUBLIC_KEY_LEN + 5]; unsigned char *data; size_t request_len; size_t data_len; - libssh2_ec_key *private_key; /* SSH2 ecdh private key */ + ssh2_ec_key *private_key; /* SSH2 ecdh private key */ unsigned char *public_key_oct; /* SSH2 ecdh public key octal value */ size_t public_key_oct_len; /* SSH2 ecdh public key octal value length */ @@ -368,7 +369,7 @@ struct key_exchange_state_low { }; struct key_exchange_state { - libssh2_nonblocking_states state; + ssh2_NB_states state; struct packet_require_state req_state; struct key_exchange_state_low key_state_low; unsigned char *data; @@ -380,7 +381,7 @@ struct key_exchange_state { #define FwdNotReq "Forward not requested" struct packet_queue_listener_state { - libssh2_nonblocking_states state; + ssh2_NB_states state; unsigned char packet[17 + (sizeof(FwdNotReq) - 1)]; unsigned char *host; unsigned char *shost; @@ -397,7 +398,7 @@ struct packet_queue_listener_state { #define X11FwdUnAvil "X11 Forward Unavailable" struct packet_x11_open_state { - libssh2_nonblocking_states state; + ssh2_NB_states state; unsigned char packet[17 + (sizeof(X11FwdUnAvil) - 1)]; unsigned char *shost; uint32_t sender_channel; @@ -411,7 +412,7 @@ struct packet_x11_open_state { #define AuthAgentUnavail "Auth Agent unavailable" struct packet_authagent_state { - libssh2_nonblocking_states state; + ssh2_NB_states state; unsigned char packet[17 + (sizeof(AuthAgentUnavail) - 1)]; uint32_t sender_channel; uint32_t initial_window_size; @@ -466,7 +467,7 @@ struct _LIBSSH2_CHANNEL { LIBSSH2_CHANNEL_CLOSE_FUNC(*close_cb); /* State variables used in libssh2_channel_setenv_ex() */ - libssh2_nonblocking_states setenv_state; + ssh2_NB_states setenv_state; unsigned char *setenv_packet; size_t setenv_packet_len; unsigned char setenv_local_channel[4]; @@ -474,73 +475,73 @@ struct _LIBSSH2_CHANNEL { /* State variables used in libssh2_channel_request_pty_ex() libssh2_channel_request_pty_size_ex() */ - libssh2_nonblocking_states reqPTY_state; + ssh2_NB_states reqPTY_state; unsigned char reqPTY_packet[41 + 256]; size_t reqPTY_packet_len; unsigned char reqPTY_local_channel[4]; struct packet_requirev_state reqPTY_packet_requirev_state; /* State variables used in libssh2_channel_x11_req_ex() */ - libssh2_nonblocking_states reqX11_state; + ssh2_NB_states reqX11_state; unsigned char *reqX11_packet; size_t reqX11_packet_len; unsigned char reqX11_local_channel[4]; struct packet_requirev_state reqX11_packet_requirev_state; /* State variables used in libssh2_channel_process_startup() */ - libssh2_nonblocking_states process_state; + ssh2_NB_states process_state; unsigned char *process_packet; size_t process_packet_len; unsigned char process_local_channel[4]; struct packet_requirev_state process_packet_requirev_state; /* State variables used in libssh2_channel_flush_ex() */ - libssh2_nonblocking_states flush_state; + ssh2_NB_states flush_state; size_t flush_refund_bytes; size_t flush_flush_bytes; /* State variables used in libssh2_channel_receive_window_adjust2() */ - libssh2_nonblocking_states adjust_state; + ssh2_NB_states adjust_state; unsigned char adjust_adjust[9]; /* packet_type(1) + channel(4) + adjustment(4) */ /* State variables used in libssh2_channel_read_ex() */ - libssh2_nonblocking_states read_state; + ssh2_NB_states read_state; uint32_t read_local_id; /* State variables used in libssh2_channel_write_ex() */ - libssh2_nonblocking_states write_state; + ssh2_NB_states write_state; unsigned char write_packet[13]; size_t write_packet_len; size_t write_bufwrite; /* State variables used in libssh2_channel_close() */ - libssh2_nonblocking_states close_state; + ssh2_NB_states close_state; unsigned char close_packet[5]; /* State variables used in libssh2_channel_wait_closedeof() */ - libssh2_nonblocking_states wait_eof_state; + ssh2_NB_states wait_eof_state; /* State variables used in libssh2_channel_wait_closed() */ - libssh2_nonblocking_states wait_closed_state; + ssh2_NB_states wait_closed_state; /* State variables used in libssh2_channel_free() */ - libssh2_nonblocking_states free_state; + ssh2_NB_states free_state; /* State variables used in libssh2_channel_handle_extended_data2() */ - libssh2_nonblocking_states extData2_state; + ssh2_NB_states extData2_state; /* State variables used in libssh2_channel_request_auth_agent() */ - libssh2_nonblocking_states req_auth_agent_try_state; - libssh2_nonblocking_states req_auth_agent_state; + ssh2_NB_states req_auth_agent_try_state; + ssh2_NB_states req_auth_agent_state; unsigned char req_auth_agent_packet[36]; size_t req_auth_agent_packet_len; unsigned char req_auth_agent_local_channel[4]; struct packet_requirev_state req_auth_agent_requirev_state; /* State variables used in libssh2_channel_signal_ex() */ - libssh2_nonblocking_states sendsignal_state; + ssh2_NB_states sendsignal_state; unsigned char *sendsignal_packet; size_t sendsignal_packet_len; }; @@ -560,7 +561,7 @@ struct _LIBSSH2_LISTENER { int queue_maxsize; /* State variables used in libssh2_channel_forward_cancel() */ - libssh2_nonblocking_states chanFwdCncl_state; + ssh2_NB_states chanFwdCncl_state; unsigned char *chanFwdCncl_data; size_t chanFwdCncl_data_len; }; @@ -609,7 +610,7 @@ struct transportpacket { number of bytes. A full package is packet_length + padding_length + 4 + mac_length. */ - unsigned char *payload; /* this is a pointer to a LIBSSH2_ALLOC() + unsigned char *payload; /* this is a pointer to a SSH2_ALLOC() area to which we write incoming packet data which is not yet decrypted in etm mode. */ unsigned char *wptr; /* write pointer into the payload to where we @@ -629,30 +630,30 @@ struct _LIBSSH2_PUBLICKEY { LIBSSH2_CHANNEL *channel; uint32_t version; - /* State variables used in libssh2_publickey_packet_receive() */ - libssh2_nonblocking_states receive_state; + /* State variables used in publickey_packet_receive() */ + ssh2_NB_states receive_state; unsigned char *receive_packet; size_t receive_packet_len; /* State variables used in libssh2_publickey_add_ex() */ - libssh2_nonblocking_states add_state; + ssh2_NB_states add_state; unsigned char *add_packet; unsigned char *add_s; /* State variables used in libssh2_publickey_remove_ex() */ - libssh2_nonblocking_states remove_state; + ssh2_NB_states remove_state; unsigned char *remove_packet; unsigned char *remove_s; /* State variables used in libssh2_publickey_list_fetch() */ - libssh2_nonblocking_states listFetch_state; + ssh2_NB_states listFetch_state; unsigned char *listFetch_s; unsigned char listFetch_buffer[12]; unsigned char *listFetch_data; size_t listFetch_data_len; }; -#define LIBSSH2_SCP_RESPONSE_BUFLEN 256 +#define SSH2_SCP_RESPONSE_BUFLEN 256 struct flags { int sigpipe; /* LIBSSH2_FLAG_SIGPIPE */ @@ -770,17 +771,17 @@ struct _LIBSSH2_SESSION { #endif /* State variables used in libssh2_banner_send() */ - libssh2_nonblocking_states banner_TxRx_state; + ssh2_NB_states banner_TxRx_state; char banner_TxRx_banner[8192]; ssize_t banner_TxRx_total_send; - /* State variables used in libssh2_kexinit() */ - libssh2_nonblocking_states kexinit_state; + /* State variables used in kexinit() */ + ssh2_NB_states kexinit_state; unsigned char *kexinit_data; size_t kexinit_data_len; /* State variables used in libssh2_session_handshake() */ - libssh2_nonblocking_states startup_state; + ssh2_NB_states startup_state; unsigned char *startup_data; size_t startup_data_len; unsigned char startup_service[sizeof("ssh-userauth") + 5 - 1]; @@ -789,26 +790,26 @@ struct _LIBSSH2_SESSION { struct key_exchange_state startup_key_state; /* State variables used in libssh2_session_free() */ - libssh2_nonblocking_states free_state; + ssh2_NB_states free_state; /* State variables used in libssh2_session_disconnect_ex() */ - libssh2_nonblocking_states disconnect_state; + ssh2_NB_states disconnect_state; unsigned char disconnect_data[256 + 13]; size_t disconnect_data_len; - /* State variables used in libssh2_packet_read() */ - libssh2_nonblocking_states readPack_state; + /* State variables used in ssh2_packet_add() */ + ssh2_NB_states readPack_state; int readPack_encrypted; /* State variables used in libssh2_userauth_list() */ - libssh2_nonblocking_states userauth_list_state; + ssh2_NB_states userauth_list_state; unsigned char *userauth_list_data; size_t userauth_list_data_len; char *userauth_banner; struct packet_requirev_state userauth_list_packet_requirev_state; /* State variables used in libssh2_userauth_password_ex() */ - libssh2_nonblocking_states userauth_pswd_state; + ssh2_NB_states userauth_pswd_state; unsigned char *userauth_pswd_data; unsigned char userauth_pswd_data0; size_t userauth_pswd_data_len; @@ -817,7 +818,7 @@ struct _LIBSSH2_SESSION { struct packet_requirev_state userauth_pswd_packet_requirev_state; /* State variables used in libssh2_userauth_hostbased_fromfile_ex() */ - libssh2_nonblocking_states userauth_host_state; + ssh2_NB_states userauth_host_state; unsigned char *userauth_host_data; size_t userauth_host_data_len; unsigned char *userauth_host_packet; @@ -828,7 +829,7 @@ struct _LIBSSH2_SESSION { struct packet_requirev_state userauth_host_packet_requirev_state; /* State variables used in libssh2_userauth_publickey_fromfile_ex() */ - libssh2_nonblocking_states userauth_pblc_state; + ssh2_NB_states userauth_pblc_state; unsigned char *userauth_pblc_data; size_t userauth_pblc_data_len; unsigned char *userauth_pblc_packet; @@ -840,7 +841,7 @@ struct _LIBSSH2_SESSION { struct packet_requirev_state userauth_pblc_packet_requirev_state; /* State variables used in libssh2_userauth_keyboard_interactive_ex() */ - libssh2_nonblocking_states userauth_kybd_state; + ssh2_NB_states userauth_kybd_state; unsigned char *userauth_kybd_data; size_t userauth_kybd_data_len; unsigned char *userauth_kybd_packet; @@ -856,7 +857,7 @@ struct _LIBSSH2_SESSION { struct packet_requirev_state userauth_kybd_packet_requirev_state; /* State variables used in libssh2_channel_open_ex() */ - libssh2_nonblocking_states open_state; + ssh2_NB_states open_state; struct packet_requirev_state open_packet_requirev_state; LIBSSH2_CHANNEL *open_channel; unsigned char *open_packet; @@ -866,21 +867,21 @@ struct _LIBSSH2_SESSION { uint32_t open_local_channel; /* State variables used in libssh2_channel_direct_tcpip_ex() */ - libssh2_nonblocking_states direct_state; + ssh2_NB_states direct_state; unsigned char *direct_message; size_t direct_host_len; size_t direct_shost_len; size_t direct_message_len; /* State variables used in libssh2_channel_forward_listen_ex() */ - libssh2_nonblocking_states fwdLstn_state; + ssh2_NB_states fwdLstn_state; unsigned char *fwdLstn_packet; uint32_t fwdLstn_host_len; uint32_t fwdLstn_packet_len; struct packet_requirev_state fwdLstn_packet_requirev_state; /* State variables used in libssh2_publickey_init() */ - libssh2_nonblocking_states pkeyInit_state; + ssh2_NB_states pkeyInit_state; LIBSSH2_PUBLICKEY *pkeyInit_pkey; LIBSSH2_CHANNEL *pkeyInit_channel; unsigned char *pkeyInit_data; @@ -889,8 +890,8 @@ struct _LIBSSH2_SESSION { unsigned char pkeyInit_buffer[19]; size_t pkeyInit_buffer_sent; /* how much of buffer that has been sent */ - /* State variables used in libssh2_packet_add() */ - libssh2_nonblocking_states packAdd_state; + /* State variables used in ssh2_packet_add() */ + ssh2_NB_states packAdd_state; LIBSSH2_CHANNEL *packAdd_channelp; /* keeper of the channel during EAGAIN states */ struct packet_queue_listener_state packAdd_Qlstn_state; @@ -898,14 +899,14 @@ struct _LIBSSH2_SESSION { struct packet_authagent_state packAdd_authagent_state; /* State variables used in fullpacket() */ - libssh2_nonblocking_states fullpacket_state; + ssh2_NB_states fullpacket_state; int fullpacket_macstate; size_t fullpacket_payload_len; int fullpacket_packet_type; uint32_t fullpacket_required_type; /* State variables used in libssh2_sftp_init() */ - libssh2_nonblocking_states sftpInit_state; + ssh2_NB_states sftpInit_state; LIBSSH2_SFTP *sftpInit_sftp; LIBSSH2_CHANNEL *sftpInit_channel; unsigned char sftpInit_buffer[9]; /* sftp_header(5){excludes request_id} @@ -914,10 +915,10 @@ struct _LIBSSH2_SESSION { sent */ /* State variables used in libssh2_scp_recv2() */ - libssh2_nonblocking_states scpRecv_state; + ssh2_NB_states scpRecv_state; unsigned char *scpRecv_command; size_t scpRecv_command_len; - unsigned char scpRecv_response[LIBSSH2_SCP_RESPONSE_BUFLEN]; + unsigned char scpRecv_response[SSH2_SCP_RESPONSE_BUFLEN]; size_t scpRecv_response_len; long scpRecv_mode; libssh2_int64_t scpRecv_size; @@ -926,10 +927,10 @@ struct _LIBSSH2_SESSION { LIBSSH2_CHANNEL *scpRecv_channel; /* State variables used in libssh2_scp_send_ex() */ - libssh2_nonblocking_states scpSend_state; + ssh2_NB_states scpSend_state; unsigned char *scpSend_command; size_t scpSend_command_len; - unsigned char scpSend_response[LIBSSH2_SCP_RESPONSE_BUFLEN]; + unsigned char scpSend_response[SSH2_SCP_RESPONSE_BUFLEN]; size_t scpSend_response_len; LIBSSH2_CHANNEL *scpSend_channel; @@ -943,22 +944,22 @@ struct _LIBSSH2_SESSION { }; /* session.state bits */ -#define LIBSSH2_STATE_INITIAL_KEX 0x00000001 -#define LIBSSH2_STATE_EXCHANGING_KEYS 0x00000002 -#define LIBSSH2_STATE_NEWKEYS 0x00000004 -#define LIBSSH2_STATE_AUTHENTICATED 0x00000008 -#define LIBSSH2_STATE_KEX_ACTIVE 0x00000010 +#define SSH2_STATE_INITIAL_KEX 0x00000001 +#define SSH2_STATE_EXCHANGING_KEYS 0x00000002 +#define SSH2_STATE_NEWKEYS 0x00000004 +#define SSH2_STATE_AUTHENTICATED 0x00000008 +#define SSH2_STATE_KEX_ACTIVE 0x00000010 /* session.flag helpers */ #ifdef MSG_NOSIGNAL -#define LIBSSH2_SOCKET_SEND_FLAGS(session) \ +#define SSH2_SOCKET_SEND_FLAGS(session) \ ((session)->flag.sigpipe ? 0 : MSG_NOSIGNAL) -#define LIBSSH2_SOCKET_RECV_FLAGS(session) \ +#define SSH2_SOCKET_RECV_FLAGS(session) \ ((session)->flag.sigpipe ? 0 : MSG_NOSIGNAL) #else /* If MSG_NOSIGNAL is not defined we are SOL on blocking SIGPIPE */ -#define LIBSSH2_SOCKET_SEND_FLAGS(session) 0 -#define LIBSSH2_SOCKET_RECV_FLAGS(session) 0 +#define SSH2_SOCKET_SEND_FLAGS(session) 0 +#define SSH2_SOCKET_RECV_FLAGS(session) 0 #endif /* --------- */ @@ -1032,27 +1033,27 @@ struct crypt_method { int firstlast); int (*dtor)(LIBSSH2_SESSION *session, void **abstract); - LIBSSH2_CIPHER_T(algo); + SSH2_CIPHER_T(algo); }; /* Bit flags for struct crypt_method */ /* Crypto method has integrated message authentication */ -#define LIBSSH2_CRYPT_FLAG_INTEGRATED_MAC 1 +#define SSH2_CRYPT_FLAG_INTEGRATED_MAC 1 /* Crypto method does not encrypt the packet length */ -#define LIBSSH2_CRYPT_FLAG_PKTLEN_AAD 2 +#define SSH2_CRYPT_FLAG_PKTLEN_AAD 2 /* Crypto method must encrypt and decrypt entire messages */ -#define LIBSSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET 4 +#define SSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET 4 /* Convenience macros for accessing crypt flags */ /* Local crypto flags */ -#define CRYPT_FLAG_L(session, flag) \ - ((session)->local.crypt && \ - ((session)->local.crypt->flags & LIBSSH2_CRYPT_FLAG_##flag)) +#define CRYPT_FLAG_L(session, flag) \ + ((session)->local.crypt && \ + ((session)->local.crypt->flags & SSH2_CRYPT_FLAG_##flag)) /* Remote crypto flags */ -#define CRYPT_FLAG_R(session, flag) \ - ((session)->remote.crypt && \ - ((session)->remote.crypt->flags & LIBSSH2_CRYPT_FLAG_##flag)) +#define CRYPT_FLAG_R(session, flag) \ + ((session)->remote.crypt && \ + ((session)->remote.crypt->flags & SSH2_CRYPT_FLAG_##flag)) /* Values for firstlast */ #define FIRST_BLOCK 1 @@ -1085,28 +1086,28 @@ struct comp_method { }; #ifdef LIBSSH2DEBUG -void _libssh2_debug_low(LIBSSH2_SESSION *session, int context, - const char *format, ...) LIBSSH2_PRINTF(3, 4); -#define _libssh2_debug(x) _libssh2_debug_low x +void ssh2_deb_low(LIBSSH2_SESSION *session, int context, + const char *format, ...) SSH2_PRINTF(3, 4); +#define ssh2_deb(x) ssh2_deb_low x #else -#define _libssh2_debug(x) do {} while(0) +#define ssh2_deb(x) do {} while(0) #endif -#define LIBSSH2_SOCKET_UNKNOWN 1 -#define LIBSSH2_SOCKET_CONNECTED 0 -#define LIBSSH2_SOCKET_DISCONNECTED (-1) +#define SSH2_SOCKET_UNKNOWN 1 +#define SSH2_SOCKET_CONNECTED 0 +#define SSH2_SOCKET_DISCONNECTED (-1) /* Initial packet state, prior to MAC check */ -#define LIBSSH2_MAC_UNCONFIRMED 1 +#define SSH2_MAC_UNCONFIRMED 1 /* When MAC type is "none" (proto initiation phase) all packets are deemed "confirmed" */ -#define LIBSSH2_MAC_CONFIRMED 0 +#define SSH2_MAC_CONFIRMED 0 /* Something bad is going on */ -#define LIBSSH2_MAC_INVALID (-1) +#define SSH2_MAC_INVALID (-1) -/* Flags for _libssh2_error_flags */ +/* Flags for ssh2_err_flags() */ /* Error message is allocated on the heap */ -#define LIBSSH2_ERR_FLAG_DUP 1 +#define SSH2_ERR_FLAG_DUP 1 /* SSH Packet Types -- Defined by Internet draft */ /* Transport Layer */ @@ -1133,7 +1134,8 @@ void _libssh2_debug_low(LIBSSH2_SESSION *session, int context, #define SSH_MSG_KEX_DH_GEX_INIT 32 #define SSH_MSG_KEX_DH_GEX_REPLY 33 -/* ecdh */ +/* ECDH + https://datatracker.ietf.org/doc/html/rfc9941 */ #define SSH2_MSG_KEX_ECDH_INIT 30 #define SSH2_MSG_KEX_ECDH_REPLY 31 @@ -1175,75 +1177,72 @@ void _libssh2_debug_low(LIBSSH2_SESSION *session, int context, #define SSH_OPEN_UNKNOWN_CHANNELTYPE 3 #define SSH_OPEN_RESOURCE_SHORTAGE 4 -ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer, - size_t length, int flags, void **abstract); -ssize_t _libssh2_send(libssh2_socket_t socket, const void *buffer, - size_t length, int flags, void **abstract); +ssize_t ssh2_recv(libssh2_socket_t socket, void *buffer, + size_t length, int flags, void **abstract); +ssize_t ssh2_send(libssh2_socket_t socket, const void *buffer, + size_t length, int flags, void **abstract); -#define LIBSSH2_DEFAULT_READ_TIMEOUT 60 /* generic timeout in seconds used when - waiting for more data to arrive */ +int ssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange, + struct key_exchange_state *key_state); -int _libssh2_kex_exchange(LIBSSH2_SESSION *session, int reexchange, - struct key_exchange_state *key_state); - -unsigned char *_libssh2_kex_agree_instr(unsigned char *haystack, - size_t haystack_len, - const unsigned char *needle, - size_t needle_len); +unsigned char *ssh2_kex_agree_instr(unsigned char *haystack, + size_t haystack_len, + const unsigned char *needle, + size_t needle_len); /* Let crypt.c/hostkey.c expose their method structs */ -const struct crypt_method **libssh2_crypt_methods(void); -const struct hostkey_method **libssh2_hostkey_methods(void); +const struct crypt_method **ssh2_crypt_methods(void); +const struct hostkey_method **ssh2_hostkey_methods(void); -int _libssh2_bcrypt_pbkdf(const char *pass, - size_t passlen, - const uint8_t *salt, - size_t saltlen, - uint8_t *key, - size_t keylen, - unsigned int rounds); +int ssh2_bcrypt_pbkdf(const char *pass, + size_t passlen, + const uint8_t *salt, + size_t saltlen, + uint8_t *key, + size_t keylen, + unsigned int rounds); /* pem.c */ -int _libssh2_pem_parse(LIBSSH2_SESSION *session, - const char *headerbegin, - const char *headerend, - const unsigned char *passphrase, - FILE *fp, unsigned char **data, size_t *datalen); -int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, - const char *headerbegin, - const char *headerend, - const unsigned char *passphrase, - const char *filedata, size_t filedata_len, - unsigned char **data, size_t *datalen); +int ssh2_pem_parse(LIBSSH2_SESSION *session, + const char *headerbegin, + const char *headerend, + const unsigned char *passphrase, + FILE *fp, unsigned char **data, size_t *datalen); +int ssh2_pem_parse_memory(LIBSSH2_SESSION *session, + const char *headerbegin, + const char *headerend, + const unsigned char *passphrase, + const char *filedata, size_t filedata_len, + unsigned char **data, size_t *datalen); /* OpenSSL keys */ -int _libssh2_openssh_pem_parse(LIBSSH2_SESSION *session, - const unsigned char *passphrase, - FILE *fp, struct string_buf **decrypted_buf); -int _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, - const unsigned char *passphrase, - const char *filedata, - size_t filedata_len, - struct string_buf **decrypted_buf); +int ssh2_openssh_pem_parse(LIBSSH2_SESSION *session, + const unsigned char *passphrase, + FILE *fp, struct string_buf **decrypted_buf); +int ssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, + const unsigned char *passphrase, + const char *filedata, + size_t filedata_len, + struct string_buf **decrypted_buf); -int _libssh2_pem_decode_sequence(unsigned char **data, size_t *datalen); -int _libssh2_pem_decode_integer(unsigned char **data, size_t *datalen, - unsigned char **i, unsigned int *ilen); +int ssh2_pem_decode_sequence(unsigned char **data, size_t *datalen); +int ssh2_pem_decode_integer(unsigned char **data, size_t *datalen, + unsigned char **i, unsigned int *ilen); /* global.c */ -void _libssh2_init_if_needed(void); +void ssh2_init_if_needed(void); /* Utility function for certificate auth */ size_t plain_method(char *method, size_t method_len); -#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#define SSH2_ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0])) /* define to output the libssh2_int64_t type in a *printf() */ #if defined(__MINGW32__) || (defined(_MSC_VER) && (_MSC_VER >= 1800)) -#define LIBSSH2_INT64_T_FORMAT PRId64 +#define SSH2_INT64_T_FORMAT PRId64 #elif defined(_WIN32) -#define LIBSSH2_INT64_T_FORMAT "I64d" +#define SSH2_INT64_T_FORMAT "I64d" #else -#define LIBSSH2_INT64_T_FORMAT "lld" +#define SSH2_INT64_T_FORMAT "lld" #endif /* In Windows the default file mode is text but an application can override it. diff --git a/src/mac.c b/src/mac.c index 2b1919a0..7739178c 100644 --- a/src/mac.c +++ b/src/mac.c @@ -93,7 +93,7 @@ static int mac_method_common_init(LIBSSH2_SESSION *session, unsigned char *key, static int mac_method_common_dtor(LIBSSH2_SESSION *session, void **abstract) { if(*abstract) { - LIBSSH2_FREE(session, *abstract); + SSH2_FREE(session, *abstract); } *abstract = NULL; @@ -111,23 +111,23 @@ static int mac_method_hmac_sha2_512_hash(LIBSSH2_SESSION *session, const unsigned char *addtl, size_t addtl_len, void **abstract) { - libssh2_hmac_ctx ctx; + ssh2_hmac_ctx ctx; unsigned char seqno_buf[4]; int res; (void)session; - _libssh2_htonu32(seqno_buf, seqno); + ssh2_htonu32(seqno_buf, seqno); - if(!_libssh2_hmac_ctx_init(&ctx)) + if(!ssh2_hmac_ctx_init(&ctx)) return 1; - res = _libssh2_hmac_sha512_init(&ctx, *abstract, 64) && - _libssh2_hmac_update(&ctx, seqno_buf, 4) && - _libssh2_hmac_update(&ctx, packet, packet_len); + res = ssh2_hmac_sha512_init(&ctx, *abstract, 64) && + ssh2_hmac_update(&ctx, seqno_buf, 4) && + ssh2_hmac_update(&ctx, packet, packet_len); if(res && addtl && addtl_len) - res = _libssh2_hmac_update(&ctx, addtl, addtl_len); + res = ssh2_hmac_update(&ctx, addtl, addtl_len); if(res) - res = _libssh2_hmac_final(&ctx, buf); - _libssh2_hmac_cleanup(&ctx); + res = ssh2_hmac_final(&ctx, buf); + ssh2_hmac_cleanup(&ctx); return !res; } @@ -165,23 +165,23 @@ static int mac_method_hmac_sha2_256_hash(LIBSSH2_SESSION *session, const unsigned char *addtl, size_t addtl_len, void **abstract) { - libssh2_hmac_ctx ctx; + ssh2_hmac_ctx ctx; unsigned char seqno_buf[4]; int res; (void)session; - _libssh2_htonu32(seqno_buf, seqno); + ssh2_htonu32(seqno_buf, seqno); - if(!_libssh2_hmac_ctx_init(&ctx)) + if(!ssh2_hmac_ctx_init(&ctx)) return 1; - res = _libssh2_hmac_sha256_init(&ctx, *abstract, 32) && - _libssh2_hmac_update(&ctx, seqno_buf, 4) && - _libssh2_hmac_update(&ctx, packet, packet_len); + res = ssh2_hmac_sha256_init(&ctx, *abstract, 32) && + ssh2_hmac_update(&ctx, seqno_buf, 4) && + ssh2_hmac_update(&ctx, packet, packet_len); if(res && addtl && addtl_len) - res = _libssh2_hmac_update(&ctx, addtl, addtl_len); + res = ssh2_hmac_update(&ctx, addtl, addtl_len); if(res) - res = _libssh2_hmac_final(&ctx, buf); - _libssh2_hmac_cleanup(&ctx); + res = ssh2_hmac_final(&ctx, buf); + ssh2_hmac_cleanup(&ctx); return !res; } @@ -218,23 +218,23 @@ static int mac_method_hmac_sha1_hash(LIBSSH2_SESSION *session, const unsigned char *addtl, size_t addtl_len, void **abstract) { - libssh2_hmac_ctx ctx; + ssh2_hmac_ctx ctx; unsigned char seqno_buf[4]; int res; (void)session; - _libssh2_htonu32(seqno_buf, seqno); + ssh2_htonu32(seqno_buf, seqno); - if(!_libssh2_hmac_ctx_init(&ctx)) + if(!ssh2_hmac_ctx_init(&ctx)) return 1; - res = _libssh2_hmac_sha1_init(&ctx, *abstract, 20) && - _libssh2_hmac_update(&ctx, seqno_buf, 4) && - _libssh2_hmac_update(&ctx, packet, packet_len); + res = ssh2_hmac_sha1_init(&ctx, *abstract, 20) && + ssh2_hmac_update(&ctx, seqno_buf, 4) && + ssh2_hmac_update(&ctx, packet, packet_len); if(res && addtl && addtl_len) - res = _libssh2_hmac_update(&ctx, addtl, addtl_len); + res = ssh2_hmac_update(&ctx, addtl, addtl_len); if(res) - res = _libssh2_hmac_final(&ctx, buf); - _libssh2_hmac_cleanup(&ctx); + res = ssh2_hmac_final(&ctx, buf); + ssh2_hmac_cleanup(&ctx); return !res; } @@ -300,23 +300,23 @@ static int mac_method_hmac_md5_hash(LIBSSH2_SESSION *session, const unsigned char *addtl, size_t addtl_len, void **abstract) { - libssh2_hmac_ctx ctx; + ssh2_hmac_ctx ctx; unsigned char seqno_buf[4]; int res; (void)session; - _libssh2_htonu32(seqno_buf, seqno); + ssh2_htonu32(seqno_buf, seqno); - if(!_libssh2_hmac_ctx_init(&ctx)) + if(!ssh2_hmac_ctx_init(&ctx)) return 1; - res = _libssh2_hmac_md5_init(&ctx, *abstract, 16) && - _libssh2_hmac_update(&ctx, seqno_buf, 4) && - _libssh2_hmac_update(&ctx, packet, packet_len); + res = ssh2_hmac_md5_init(&ctx, *abstract, 16) && + ssh2_hmac_update(&ctx, seqno_buf, 4) && + ssh2_hmac_update(&ctx, packet, packet_len); if(res && addtl && addtl_len) - res = _libssh2_hmac_update(&ctx, addtl, addtl_len); + res = ssh2_hmac_update(&ctx, addtl, addtl_len); if(res) - res = _libssh2_hmac_final(&ctx, buf); - _libssh2_hmac_cleanup(&ctx); + res = ssh2_hmac_final(&ctx, buf); + ssh2_hmac_cleanup(&ctx); return !res; } @@ -373,23 +373,23 @@ static int mac_method_hmac_ripemd160_hash(LIBSSH2_SESSION *session, const unsigned char *addtl, size_t addtl_len, void **abstract) { - libssh2_hmac_ctx ctx; + ssh2_hmac_ctx ctx; unsigned char seqno_buf[4]; int res; (void)session; - _libssh2_htonu32(seqno_buf, seqno); + ssh2_htonu32(seqno_buf, seqno); - if(!_libssh2_hmac_ctx_init(&ctx)) + if(!ssh2_hmac_ctx_init(&ctx)) return 1; - res = _libssh2_hmac_ripemd160_init(&ctx, *abstract, 20) && - _libssh2_hmac_update(&ctx, seqno_buf, 4) && - _libssh2_hmac_update(&ctx, packet, packet_len); + res = ssh2_hmac_ripemd160_init(&ctx, *abstract, 20) && + ssh2_hmac_update(&ctx, seqno_buf, 4) && + ssh2_hmac_update(&ctx, packet, packet_len); if(res && addtl && addtl_len) - res = _libssh2_hmac_update(&ctx, addtl, addtl_len); + res = ssh2_hmac_update(&ctx, addtl, addtl_len); if(res) - res = _libssh2_hmac_final(&ctx, buf); - _libssh2_hmac_cleanup(&ctx); + res = ssh2_hmac_final(&ctx, buf); + ssh2_hmac_cleanup(&ctx); return !res; } @@ -441,7 +441,7 @@ static const struct mac_method *mac_methods[] = { NULL }; -const struct mac_method **_libssh2_mac_methods(void) +const struct mac_method **ssh2_mac_methods(void) { return mac_methods; } @@ -498,8 +498,7 @@ static const struct mac_method mac_method_hmac_aesgcm = { /* See if the negotiated crypto method has its own authentication scheme that * obviates the need for a separate negotiated hmac method */ -const struct mac_method *_libssh2_mac_override( - const struct crypt_method *crypt) +const struct mac_method *ssh2_mac_override(const struct crypt_method *crypt) { #if LIBSSH2_AES_GCM if(!strcmp(crypt->name, "aes256-gcm@openssh.com") || diff --git a/src/mac.h b/src/mac.h index 7b4e21fc..1c6a082a 100644 --- a/src/mac.h +++ b/src/mac.h @@ -62,8 +62,7 @@ struct mac_method { int etm; /* encrypt-then-mac */ }; -const struct mac_method **_libssh2_mac_methods(void); -const struct mac_method *_libssh2_mac_override( - const struct crypt_method *crypt); +const struct mac_method **ssh2_mac_methods(void); +const struct mac_method *ssh2_mac_override(const struct crypt_method *crypt); #endif /* LIBSSH2_MAC_H */ diff --git a/src/mbedtls.c b/src/mbedtls.c index 377ecd11..976ee9f3 100644 --- a/src/mbedtls.c +++ b/src/mbedtls.c @@ -60,7 +60,7 @@ static mbedtls_ctr_drbg_context mbed_ctr_drbg; * mbedTLS backend: Generic functions */ -void _libssh2_mbedtls_init(void) +void ssh2_mbed_init(void) { int ret; @@ -74,13 +74,13 @@ void _libssh2_mbedtls_init(void) mbedtls_ctr_drbg_free(&mbed_ctr_drbg); } -void _libssh2_mbedtls_free(void) +void ssh2_mbed_free(void) { mbedtls_ctr_drbg_free(&mbed_ctr_drbg); mbedtls_entropy_free(&mbed_entropy); } -int _libssh2_mbedtls_random(unsigned char *buf, size_t len) +int ssh2_mbed_random(unsigned char *buf, size_t len) { int ret; ret = mbedtls_ctr_drbg_random(&mbed_ctr_drbg, buf, len); @@ -93,16 +93,14 @@ static void mbed_safe_free(void *buf, size_t len) return; if(len > 0) - _libssh2_explicit_zero(buf, len); + ssh2_explicit_zero(buf, len); mbedtls_free(buf); } -int _libssh2_mbedtls_cipher_init(libssh2_cipher_ctx *h, - LIBSSH2_CIPHER_T(algo), - unsigned char *iv, - unsigned char *secret, - int encrypt) +int ssh2_mbed_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo), + unsigned char *iv, unsigned char *secret, + int encrypt) { const mbedtls_cipher_info_t *cipher_info; int ret, op; @@ -145,11 +143,9 @@ int _libssh2_mbedtls_cipher_init(libssh2_cipher_ctx *h, return ret == 0 ? 0 : -1; } -int _libssh2_mbedtls_cipher_crypt(libssh2_cipher_ctx *ctx, - LIBSSH2_CIPHER_T(algo), - int encrypt, - unsigned char *block, - size_t blocksize, int firstlast) +int ssh2_mbed_cipher_crypt(ssh2_cipher_ctx *ctx, SSH2_CIPHER_T(algo), + int encrypt, unsigned char *block, size_t blocksize, + int firstlast) { int ret; unsigned char *output; @@ -184,14 +180,14 @@ int _libssh2_mbedtls_cipher_crypt(libssh2_cipher_ctx *ctx, return ret == 0 ? 0 : -1; } -void _libssh2_mbedtls_cipher_dtor(libssh2_cipher_ctx *ctx) +void ssh2_mbed_cipher_dtor(ssh2_cipher_ctx *ctx) { mbedtls_cipher_free(ctx); } -int _libssh2_mbedtls_hash_init(mbedtls_md_context_t *ctx, - mbedtls_md_type_t mdtype, - const unsigned char *key, size_t keylen) +int ssh2_mbed_hash_init(mbedtls_md_context_t *ctx, + mbedtls_md_type_t mdtype, + const unsigned char *key, size_t keylen) { const mbedtls_md_info_t *md_info; int ret, hmac; @@ -214,7 +210,7 @@ int _libssh2_mbedtls_hash_init(mbedtls_md_context_t *ctx, return ret == 0 ? 1 : 0; } -int _libssh2_mbedtls_hash_final(mbedtls_md_context_t *ctx, unsigned char *hash) +int ssh2_mbed_hash_final(mbedtls_md_context_t *ctx, unsigned char *hash) { int ret; @@ -224,8 +220,8 @@ int _libssh2_mbedtls_hash_final(mbedtls_md_context_t *ctx, unsigned char *hash) return ret == 0 ? 1 : 0; } -int _libssh2_mbedtls_hash(const unsigned char *data, size_t datalen, - mbedtls_md_type_t mdtype, unsigned char *hash) +int ssh2_mbed_hash(const unsigned char *data, size_t datalen, + mbedtls_md_type_t mdtype, unsigned char *hash) { const mbedtls_md_info_t *md_info; int ret; @@ -239,58 +235,56 @@ int _libssh2_mbedtls_hash(const unsigned char *data, size_t datalen, return ret == 0 ? 0 : -1; } -int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx) +int ssh2_hmac_ctx_init(ssh2_hmac_ctx *ctx) { memset(ctx, 0, sizeof(*ctx)); return 1; } #if LIBSSH2_MD5 -int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_md5_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - return _libssh2_mbedtls_hash_init(ctx, MBEDTLS_MD_MD5, key, keylen); + return ssh2_mbed_hash_init(ctx, MBEDTLS_MD_MD5, key, keylen); } #endif #if LIBSSH2_HMAC_RIPEMD -int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen) +int ssh2_hmac_ripemd160_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - return _libssh2_mbedtls_hash_init(ctx, MBEDTLS_MD_RIPEMD160, key, keylen); + return ssh2_mbed_hash_init(ctx, MBEDTLS_MD_RIPEMD160, key, keylen); } #endif -int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha1_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - return _libssh2_mbedtls_hash_init(ctx, MBEDTLS_MD_SHA1, key, keylen); + return ssh2_mbed_hash_init(ctx, MBEDTLS_MD_SHA1, key, keylen); } -int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha256_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - return _libssh2_mbedtls_hash_init(ctx, MBEDTLS_MD_SHA256, key, keylen); + return ssh2_mbed_hash_init(ctx, MBEDTLS_MD_SHA256, key, keylen); } -int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha512_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - return _libssh2_mbedtls_hash_init(ctx, MBEDTLS_MD_SHA512, key, keylen); + return ssh2_mbed_hash_init(ctx, MBEDTLS_MD_SHA512, key, keylen); } -int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, - const void *data, size_t datalen) +int ssh2_hmac_update(ssh2_hmac_ctx *ctx, const void *data, size_t datalen) { int ret = mbedtls_md_hmac_update(ctx, data, datalen); return ret == 0 ? 1 : 0; } -int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data) +int ssh2_hmac_final(ssh2_hmac_ctx *ctx, void *data) { int ret = mbedtls_md_hmac_finish(ctx, data); return ret == 0 ? 1 : 0; } -void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) +void ssh2_hmac_cleanup(ssh2_hmac_ctx *ctx) { mbedtls_md_free(ctx); } @@ -300,11 +294,11 @@ void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) * mbedTLS backend: BigNumber functions */ -libssh2_bn *_libssh2_mbedtls_bignum_init(void) +ssh2_bn *ssh2_mbed_bn_init(void) { - libssh2_bn *bignum; + ssh2_bn *bignum; - bignum = (libssh2_bn *)mbedtls_calloc(1, sizeof(libssh2_bn)); + bignum = (ssh2_bn *)mbedtls_calloc(1, sizeof(ssh2_bn)); if(bignum) { mbedtls_mpi_init(bignum); } @@ -312,7 +306,7 @@ libssh2_bn *_libssh2_mbedtls_bignum_init(void) return bignum; } -void _libssh2_mbedtls_bignum_free(libssh2_bn *bn) +void ssh2_mbed_bn_free(ssh2_bn *bn) { if(bn) { mbedtls_mpi_free(bn); @@ -320,7 +314,7 @@ void _libssh2_mbedtls_bignum_free(libssh2_bn *bn) } } -static int mbed_bignum_random(libssh2_bn *bn, int bits, int top, int bottom) +static int mbed_bn_random(ssh2_bn *bn, int bits, int top, int bottom) { size_t len; int err; @@ -371,21 +365,21 @@ static int mbed_bignum_random(libssh2_bn *bn, int bits, int top, int bottom) * mbedTLS backend: RSA functions */ -int _libssh2_mbedtls_rsa_new(libssh2_rsa_ctx **rsa, - const unsigned char *edata, unsigned long elen, - const unsigned char *ndata, unsigned long nlen, - const unsigned char *ddata, unsigned long dlen, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *e1data, unsigned long e1len, - const unsigned char *e2data, unsigned long e2len, - const unsigned char *coeffdata, - unsigned long coefflen) +int ssh2_mbed_rsa_new(ssh2_rsa_ctx **rsa, + const unsigned char *edata, unsigned long elen, + const unsigned char *ndata, unsigned long nlen, + const unsigned char *ddata, unsigned long dlen, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *e1data, unsigned long e1len, + const unsigned char *e2data, unsigned long e2len, + const unsigned char *coeffdata, + unsigned long coefflen) { int ret; - libssh2_rsa_ctx *ctx; + ssh2_rsa_ctx *ctx; - ctx = (libssh2_rsa_ctx *)mbedtls_calloc(1, sizeof(libssh2_rsa_ctx)); + ctx = (ssh2_rsa_ctx *)mbedtls_calloc(1, sizeof(ssh2_rsa_ctx)); if(ctx) mbedtls_rsa_init(ctx); else @@ -426,23 +420,23 @@ int _libssh2_mbedtls_rsa_new(libssh2_rsa_ctx **rsa, } if(ret && ctx) { - _libssh2_mbedtls_rsa_free(ctx); + ssh2_mbed_rsa_free(ctx); ctx = NULL; } *rsa = ctx; return ret; } -int _libssh2_mbedtls_rsa_new_private(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_mbed_rsa_new_private(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { int ret; mbedtls_pk_context pkey; mbedtls_rsa_context *pk_rsa; - *rsa = (libssh2_rsa_ctx *)LIBSSH2_ALLOC(session, sizeof(libssh2_rsa_ctx)); + *rsa = (ssh2_rsa_ctx *)SSH2_ALLOC(session, sizeof(ssh2_rsa_ctx)); if(!*rsa) return -1; @@ -454,7 +448,7 @@ int _libssh2_mbedtls_rsa_new_private(libssh2_rsa_ctx **rsa, if(ret || mbedtls_pk_get_type(&pkey) != MBEDTLS_PK_RSA) { mbedtls_pk_free(&pkey); mbedtls_rsa_free(*rsa); - LIBSSH2_FREE(session, *rsa); + SSH2_FREE(session, *rsa); *rsa = NULL; return -1; } @@ -466,12 +460,11 @@ int _libssh2_mbedtls_rsa_new_private(libssh2_rsa_ctx **rsa, return 0; } -int _libssh2_mbedtls_rsa_new_private_frommemory( - libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_mbed_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { int ret; mbedtls_pk_context pkey; @@ -479,7 +472,7 @@ int _libssh2_mbedtls_rsa_new_private_frommemory( void *filedata_nullterm; size_t pwd_len; - *rsa = (libssh2_rsa_ctx *)mbedtls_calloc(1, sizeof(libssh2_rsa_ctx)); + *rsa = (ssh2_rsa_ctx *)mbedtls_calloc(1, sizeof(ssh2_rsa_ctx)); if(!*rsa) return -1; @@ -512,7 +505,7 @@ int _libssh2_mbedtls_rsa_new_private_frommemory( if(ret || mbedtls_pk_get_type(&pkey) != MBEDTLS_PK_RSA) { mbedtls_pk_free(&pkey); mbedtls_rsa_free(*rsa); - LIBSSH2_FREE(session, *rsa); + SSH2_FREE(session, *rsa); *rsa = NULL; return -1; } @@ -524,12 +517,12 @@ int _libssh2_mbedtls_rsa_new_private_frommemory( return 0; } -int _libssh2_mbedtls_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, - size_t hash_len, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, - size_t m_len) +int ssh2_mbed_rsa_sha2_verify(ssh2_rsa_ctx *rsactx, + size_t hash_len, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, + size_t m_len) { int ret; int md_type; @@ -555,7 +548,7 @@ int _libssh2_mbedtls_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, free(hash); return -1; /* unsupported digest */ } - ret = _libssh2_mbedtls_hash(m, m_len, md_type, hash); + ret = ssh2_mbed_hash(m, m_len, md_type, hash); if(ret) { free(hash); @@ -570,22 +563,22 @@ int _libssh2_mbedtls_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, return (ret == 0) ? 0 : -1; } -int _libssh2_mbedtls_rsa_sha1_verify(libssh2_rsa_ctx *rsactx, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, - size_t m_len) +int ssh2_mbed_rsa_sha1_verify(ssh2_rsa_ctx *rsactx, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, + size_t m_len) { - return _libssh2_mbedtls_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, - sig, sig_len, m, m_len); + return ssh2_mbed_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, + sig, sig_len, m, m_len); } -int _libssh2_mbedtls_rsa_sha2_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +int ssh2_mbed_rsa_sha2_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { int ret; unsigned char *sig; @@ -593,7 +586,7 @@ int _libssh2_mbedtls_rsa_sha2_sign(LIBSSH2_SESSION *session, int md_type; sig_len = mbedtls_rsa_get_len(rsactx); - sig = LIBSSH2_ALLOC(session, sig_len); + sig = SSH2_ALLOC(session, sig_len); if(!sig) { return -1; } @@ -608,8 +601,8 @@ int _libssh2_mbedtls_rsa_sha2_sign(LIBSSH2_SESSION *session, md_type = MBEDTLS_MD_SHA512; } else { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unsupported hash digest length"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unsupported hash digest length"); ret = -1; } if(ret == 0) @@ -619,7 +612,7 @@ int _libssh2_mbedtls_rsa_sha2_sign(LIBSSH2_SESSION *session, md_type, (unsigned int)hash_len, hash, sig); if(ret) { - LIBSSH2_FREE(session, sig); + SSH2_FREE(session, sig); return -1; } @@ -629,26 +622,26 @@ int _libssh2_mbedtls_rsa_sha2_sign(LIBSSH2_SESSION *session, return (ret == 0) ? 0 : -1; } -int _libssh2_mbedtls_rsa_sha1_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +int ssh2_mbed_rsa_sha1_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { - return _libssh2_mbedtls_rsa_sha2_sign(session, rsactx, hash, hash_len, - signature, signature_len); + return ssh2_mbed_rsa_sha2_sign(session, rsactx, hash, hash_len, + signature, signature_len); } -void _libssh2_mbedtls_rsa_free(libssh2_rsa_ctx *ctx) +void ssh2_mbed_rsa_free(ssh2_rsa_ctx *ctx) { mbedtls_rsa_free(ctx); mbedtls_free(ctx); } -static unsigned char *gen_publickey_from_rsa(LIBSSH2_SESSION *session, - mbedtls_rsa_context *rsa, - size_t *keylen) +static unsigned char *mbed_gen_publickey_from_rsa(LIBSSH2_SESSION *session, + mbedtls_rsa_context *rsa, + size_t *keylen) { uint32_t e_bytes, n_bytes; uint32_t len; @@ -661,7 +654,7 @@ static unsigned char *gen_publickey_from_rsa(LIBSSH2_SESSION *session, /* Key form is "ssh-rsa" + e + n. */ len = 4 + 7 + 4 + e_bytes + 4 + n_bytes; - key = LIBSSH2_ALLOC(session, len); + key = SSH2_ALLOC(session, len); if(!key) { return NULL; } @@ -669,18 +662,18 @@ static unsigned char *gen_publickey_from_rsa(LIBSSH2_SESSION *session, /* Process key encoding. */ p = key; - _libssh2_htonu32(p, 7); /* Key type. */ + ssh2_htonu32(p, 7); /* Key type. */ p += 4; /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */ memcpy(p, "ssh-rsa", 7); p += 7; - _libssh2_htonu32(p, e_bytes); + ssh2_htonu32(p, e_bytes); p += 4; mbedtls_mpi_write_binary(&rsa->MBEDTLS_PRIVATE(E), p, e_bytes); p += e_bytes; /* Increment write index after writing to buffer */ - _libssh2_htonu32(p, n_bytes); + ssh2_htonu32(p, n_bytes); p += 4; mbedtls_mpi_write_binary(&rsa->MBEDTLS_PRIVATE(N), p, n_bytes); p += n_bytes; /* Increment write index after writing to buffer */ @@ -703,15 +696,14 @@ static int mbed_pub_priv_key(LIBSSH2_SESSION *session, if(mbedtls_pk_get_type(pkey) != MBEDTLS_PK_RSA) { mbedtls_pk_free(pkey); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Key type not supported"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, "Key type not supported"); } ret = 0; /* write method */ mthlen = 7; - mth = LIBSSH2_ALLOC(session, mthlen); + mth = SSH2_ALLOC(session, mthlen); if(mth) { memcpy(mth, "ssh-rsa", mthlen); } @@ -720,7 +712,7 @@ static int mbed_pub_priv_key(LIBSSH2_SESSION *session, } rsa = mbedtls_pk_rsa(*pkey); - key = gen_publickey_from_rsa(session, rsa, &keylen); + key = mbed_gen_publickey_from_rsa(session, rsa, &keylen); if(!key) { ret = -1; } @@ -728,9 +720,9 @@ static int mbed_pub_priv_key(LIBSSH2_SESSION *session, /* write output */ if(ret) { if(mth) - LIBSSH2_FREE(session, mth); + SSH2_FREE(session, mth); if(key) - LIBSSH2_FREE(session, key); + SSH2_FREE(session, key); } else { *method = mth; @@ -742,13 +734,13 @@ static int mbed_pub_priv_key(LIBSSH2_SESSION *session, return ret; } -int _libssh2_mbedtls_pub_priv_keyfile(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekey, - const char *passphrase) +int ssh2_mbed_pub_priv_keyfile(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekey, + const char *passphrase) { mbedtls_pk_context pkey; char buf[1024]; @@ -761,8 +753,8 @@ int _libssh2_mbedtls_pub_priv_keyfile(LIBSSH2_SESSION *session, if(ret) { mbedtls_strerror(ret, (char *)buf, sizeof(buf)); mbedtls_pk_free(&pkey); - return _libssh2_error_flags(session, LIBSSH2_ERROR_FILE, buf, - LIBSSH2_ERR_FLAG_DUP); + return ssh2_err_flags(session, LIBSSH2_ERROR_FILE, buf, + SSH2_ERR_FLAG_DUP); } ret = mbed_pub_priv_key(session, method, method_len, @@ -773,14 +765,14 @@ int _libssh2_mbedtls_pub_priv_keyfile(LIBSSH2_SESSION *session, return ret; } -int _libssh2_mbedtls_pub_priv_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_mbed_pub_priv_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { mbedtls_pk_context pkey; char buf[1024]; @@ -812,8 +804,8 @@ int _libssh2_mbedtls_pub_priv_keyfilememory(LIBSSH2_SESSION *session, if(ret) { mbedtls_strerror(ret, (char *)buf, sizeof(buf)); mbedtls_pk_free(&pkey); - return _libssh2_error_flags(session, LIBSSH2_ERROR_FILE, buf, - LIBSSH2_ERR_FLAG_DUP); + return ssh2_err_flags(session, LIBSSH2_ERROR_FILE, buf, + SSH2_ERR_FLAG_DUP); } ret = mbed_pub_priv_key(session, method, method_len, @@ -824,19 +816,19 @@ int _libssh2_mbedtls_pub_priv_keyfilememory(LIBSSH2_SESSION *session, return ret; } -int _libssh2_mbedtls_sk_pub_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - int *algorithm, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_mbed_sk_pub_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + int *algorithm, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { (void)method; (void)method_len; @@ -851,14 +843,9 @@ int _libssh2_mbedtls_sk_pub_keyfilememory(LIBSSH2_SESSION *session, (void)privatekeydata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public SK key from private key " - "file: Method unimplemented in mbedTLS backend"); -} - -void _libssh2_init_aes_ctr(void) -{ - /* no implementation */ + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public SK key from private key " + "file: Method unimplemented in mbedTLS backend"); } /*******************************************************************/ @@ -866,31 +853,31 @@ void _libssh2_init_aes_ctr(void) * mbedTLS backend: Diffie-Hellman functions */ -void _libssh2_dh_init(libssh2_dh_ctx *dhctx) +void ssh2_mbed_dh_init(ssh2_dh_ctx *dhctx) { - *dhctx = _libssh2_mbedtls_bignum_init(); /* Random from client */ + *dhctx = ssh2_mbed_bn_init(); /* Random from client */ } -int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, int group_order) +int ssh2_mbed_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, int group_order) { /* Generate x and e */ - mbed_bignum_random(*dhctx, group_order * 8 - 1, 0, -1); + mbed_bn_random(*dhctx, group_order * 8 - 1, 0, -1); mbedtls_mpi_exp_mod(public, g, *dhctx, p, NULL); return 0; } -int _libssh2_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p) +int ssh2_mbed_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p) { /* Compute the shared secret */ mbedtls_mpi_exp_mod(secret, f, *dhctx, p, NULL); return 0; } -void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx) +void ssh2_mbed_dh_dtor(ssh2_dh_ctx *dhctx) { - _libssh2_mbedtls_bignum_free(*dhctx); + ssh2_mbed_bn_free(*dhctx); *dhctx = NULL; } @@ -905,15 +892,15 @@ void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx) * Creates a local private key based on input curve * and returns octal value and octal length */ -int _libssh2_mbedtls_ecdsa_create_key(LIBSSH2_SESSION *session, - libssh2_ec_key **out_private_key, - unsigned char **out_public_key_octal, - size_t *out_public_key_octal_len, - libssh2_curve_type curve_type) +int ssh2_mbed_ecdsa_create_key(LIBSSH2_SESSION *session, + ssh2_ec_key **out_private_key, + unsigned char **out_public_key_octal, + size_t *out_public_key_octal_len, + ssh2_curve_type curve_type) { size_t plen = 0; - *out_private_key = LIBSSH2_ALLOC(session, sizeof(mbedtls_ecp_keypair)); + *out_private_key = SSH2_ALLOC(session, sizeof(mbedtls_ecp_keypair)); if(!*out_private_key) goto failed; @@ -926,7 +913,7 @@ int _libssh2_mbedtls_ecdsa_create_key(LIBSSH2_SESSION *session, plen = 2 * mbedtls_mpi_size( &(*out_private_key)->MBEDTLS_PRIVATE(grp).P) + 1; - *out_public_key_octal = LIBSSH2_ALLOC(session, plen); + *out_public_key_octal = SSH2_ALLOC(session, plen); if(!*out_public_key_octal) goto failed; @@ -940,7 +927,7 @@ int _libssh2_mbedtls_ecdsa_create_key(LIBSSH2_SESSION *session, failed: - _libssh2_mbedtls_ecdsa_free(*out_private_key); + ssh2_mbed_ecdsa_free(*out_private_key); mbed_safe_free(*out_public_key_octal, plen); *out_private_key = NULL; @@ -950,11 +937,10 @@ failed: /* * Creates a new public key given an octal string, length and type */ -int _libssh2_mbedtls_ecdsa_curve_name_with_octal_new( - libssh2_ecdsa_ctx **ec_ctx, - const unsigned char *k, - size_t k_len, - libssh2_curve_type curve) +int ssh2_mbed_ecdsa_curve_name_with_octal_new(ssh2_ecdsa_ctx **ec_ctx, + const unsigned char *k, + size_t k_len, + ssh2_curve_type curve) { *ec_ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); @@ -977,7 +963,7 @@ int _libssh2_mbedtls_ecdsa_curve_name_with_octal_new( failed: - _libssh2_mbedtls_ecdsa_free(*ec_ctx); + ssh2_mbed_ecdsa_free(*ec_ctx); *ec_ctx = NULL; return -1; @@ -987,10 +973,10 @@ failed: * Computes the shared secret K given a local private key, * remote public key and length */ -int _libssh2_mbedtls_ecdh_gen_k(libssh2_bn **k, - libssh2_ec_key *private_key, - const unsigned char *server_public_key, - size_t server_public_key_len) +int ssh2_mbed_ecdh_gen_k(ssh2_bn **k, + ssh2_ec_key *private_key, + const unsigned char *server_public_key, + size_t server_public_key_len) { mbedtls_ecp_point pubkey; int rc = 0; @@ -1027,11 +1013,11 @@ cleanup: return rc; } -#define LIBSSH2_MBEDTLS_ECDSA_VERIFY(digest_type) \ +#define SSH2_MBED_ECDSA_VERIFY(digest_type) \ do { \ unsigned char hsh[SHA##digest_type##_DIGEST_LENGTH]; \ \ - if(libssh2_sha##digest_type(m, m_len, hsh) == 0) { \ + if(ssh2_sha##digest_type(m, m_len, hsh) == 0) { \ rc = mbedtls_ecdsa_verify(&ec_ctx->MBEDTLS_PRIVATE(grp), hsh, \ SHA##digest_type##_DIGEST_LENGTH, \ &ec_ctx->MBEDTLS_PRIVATE(Q), &pr, &ps); \ @@ -1041,10 +1027,10 @@ cleanup: /* * Verifies the ECDSA signature of a hashed message */ -int _libssh2_mbedtls_ecdsa_verify(libssh2_ecdsa_ctx *ec_ctx, - const unsigned char *r, size_t r_len, - const unsigned char *s, size_t s_len, - const unsigned char *m, size_t m_len) +int ssh2_mbed_ecdsa_verify(ssh2_ecdsa_ctx *ec_ctx, + const unsigned char *r, size_t r_len, + const unsigned char *s, size_t s_len, + const unsigned char *m, size_t m_len) { mbedtls_mpi pr, ps; int rc = -1; @@ -1058,15 +1044,15 @@ int _libssh2_mbedtls_ecdsa_verify(libssh2_ecdsa_ctx *ec_ctx, if(mbedtls_mpi_read_binary(&ps, s, s_len)) goto cleanup; - switch(_libssh2_ecdsa_get_curve_type(ec_ctx)) { - case LIBSSH2_EC_CURVE_NISTP256: - LIBSSH2_MBEDTLS_ECDSA_VERIFY(256); + switch(ssh2_ecdsa_get_curve_type(ec_ctx)) { + case SSH2_EC_CURVE_NISTP256: + SSH2_MBED_ECDSA_VERIFY(256); break; - case LIBSSH2_EC_CURVE_NISTP384: - LIBSSH2_MBEDTLS_ECDSA_VERIFY(384); + case SSH2_EC_CURVE_NISTP384: + SSH2_MBED_ECDSA_VERIFY(384); break; - case LIBSSH2_EC_CURVE_NISTP521: - LIBSSH2_MBEDTLS_ECDSA_VERIFY(512); + case SSH2_EC_CURVE_NISTP521: + SSH2_MBED_ECDSA_VERIFY(512); break; default: rc = -1; @@ -1080,7 +1066,7 @@ cleanup: return (rc == 0) ? 0 : -1; } -static int mbed_parse_eckey(libssh2_ecdsa_ctx **ctx, +static int mbed_parse_eckey(ssh2_ecdsa_ctx **ctx, mbedtls_pk_context *pkey, LIBSSH2_SESSION *session, const unsigned char *data, @@ -1099,7 +1085,7 @@ static int mbed_parse_eckey(libssh2_ecdsa_ctx **ctx, if(mbedtls_pk_get_type(pkey) != MBEDTLS_PK_ECKEY) goto failed; - *ctx = LIBSSH2_ALLOC(session, sizeof(libssh2_ecdsa_ctx)); + *ctx = SSH2_ALLOC(session, sizeof(ssh2_ecdsa_ctx)); if(!*ctx) goto failed; @@ -1111,45 +1097,45 @@ static int mbed_parse_eckey(libssh2_ecdsa_ctx **ctx, failed: - _libssh2_mbedtls_ecdsa_free(*ctx); + ssh2_mbed_ecdsa_free(*ctx); *ctx = NULL; return -1; } -static int mbed_parse_openssh_key(libssh2_ecdsa_ctx **ctx, +static int mbed_parse_openssh_key(ssh2_ecdsa_ctx **ctx, LIBSSH2_SESSION *session, const unsigned char *data, size_t data_len, const unsigned char *pwd) { - libssh2_curve_type type; + ssh2_curve_type type; unsigned char *name = NULL; struct string_buf *decrypted = NULL; size_t curvelen, exponentlen, pointlen; unsigned char *curve, *exponent, *point_buf; - if(_libssh2_openssh_pem_parse_memory(session, pwd, - (const char *)data, data_len, - &decrypted)) + if(ssh2_openssh_pem_parse_memory(session, pwd, + (const char *)data, data_len, + &decrypted)) goto failed; - if(_libssh2_get_string(decrypted, &name, NULL)) + if(ssh2_get_string(decrypted, &name, NULL)) goto failed; - if(_libssh2_mbedtls_ecdsa_curve_type_from_name((const char *)name, &type)) + if(ssh2_mbed_ecdsa_curve_type_from_name((const char *)name, &type)) goto failed; - if(_libssh2_get_string(decrypted, &curve, &curvelen)) + if(ssh2_get_string(decrypted, &curve, &curvelen)) goto failed; - if(_libssh2_get_string(decrypted, &point_buf, &pointlen)) + if(ssh2_get_string(decrypted, &point_buf, &pointlen)) goto failed; - if(_libssh2_get_bignum_bytes(decrypted, &exponent, &exponentlen)) + if(ssh2_get_bignum_bytes(decrypted, &exponent, &exponentlen)) goto failed; - *ctx = LIBSSH2_ALLOC(session, sizeof(libssh2_ecdsa_ctx)); + *ctx = SSH2_ALLOC(session, sizeof(ssh2_ecdsa_ctx)); if(!*ctx) goto failed; @@ -1177,13 +1163,13 @@ static int mbed_parse_openssh_key(libssh2_ecdsa_ctx **ctx, failed: - _libssh2_mbedtls_ecdsa_free(*ctx); + ssh2_mbed_ecdsa_free(*ctx); *ctx = NULL; cleanup: if(decrypted) { - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); } return *ctx ? 0 : -1; @@ -1192,10 +1178,10 @@ cleanup: /* * Creates a new private key given a file path and password */ -int _libssh2_mbedtls_ecdsa_new_private(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_mbed_ecdsa_new_private(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { mbedtls_pk_context pkey; unsigned char *data = NULL; @@ -1218,7 +1204,7 @@ int _libssh2_mbedtls_ecdsa_new_private(libssh2_ecdsa_ctx **ec_ctx, data_len = (size_t)file_size; if(data_len == 0) goto cleanup; - data = LIBSSH2_ALLOC(session, data_len); + data = SSH2_ALLOC(session, data_len); if(!data) goto cleanup; if(fread(data, 1, data_len, fp) != data_len) @@ -1235,8 +1221,8 @@ cleanup: if(fp) fclose(fp); if(data) { - _libssh2_explicit_zero(data, data_len); - LIBSSH2_FREE(session, data); + ssh2_explicit_zero(data, data_len); + SSH2_FREE(session, data); } mbedtls_pk_free(&pkey); @@ -1247,19 +1233,18 @@ cleanup: /* * Creates a new private key given a file data and password */ -int _libssh2_mbedtls_ecdsa_new_private_frommemory( - libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_mbed_ecdsa_new_private_frommemory(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { unsigned char *ntdata; mbedtls_pk_context pkey; mbedtls_pk_init(&pkey); - ntdata = LIBSSH2_ALLOC(session, filedata_len + 1); + ntdata = SSH2_ALLOC(session, filedata_len + 1); if(!ntdata) goto cleanup; @@ -1304,7 +1289,7 @@ static unsigned char *mbed_mpi_write_binary(unsigned char *buf, memmove(p, p + 1, --size); } - _libssh2_htonu32(p - 4, size); + ssh2_htonu32(p - 4, size); done: @@ -1314,12 +1299,12 @@ done: /* * Computes the ECDSA signature of a previously-hashed message */ -int _libssh2_mbedtls_ecdsa_sign(LIBSSH2_SESSION *session, - libssh2_ecdsa_ctx *ec_ctx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +int ssh2_mbed_ecdsa_sign(LIBSSH2_SESSION *session, + ssh2_ecdsa_ctx *ec_ctx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { size_t r_len, s_len, tmp_sign_len = 0; unsigned char *sp, *tmp_sign = NULL; @@ -1339,7 +1324,7 @@ int _libssh2_mbedtls_ecdsa_sign(LIBSSH2_SESSION *session, s_len = mbedtls_mpi_size(&ps) + 1; tmp_sign_len = r_len + s_len + 8; - tmp_sign = LIBSSH2_CALLOC(session, tmp_sign_len); + tmp_sign = SSH2_CALLOC(session, tmp_sign_len); if(!tmp_sign) goto cleanup; @@ -1350,7 +1335,7 @@ int _libssh2_mbedtls_ecdsa_sign(LIBSSH2_SESSION *session, *signature_len = (size_t)(sp - tmp_sign); - *signature = LIBSSH2_CALLOC(session, *signature_len); + *signature = SSH2_CALLOC(session, *signature_len); if(!*signature) goto cleanup; @@ -1368,32 +1353,31 @@ cleanup: } /* - * returns key curve type that maps to libssh2_curve_type + * returns key curve type that maps to ssh2_curve_type */ -libssh2_curve_type _libssh2_mbedtls_ecdsa_get_curve_type( - libssh2_ecdsa_ctx *ec_ctx) +ssh2_curve_type ssh2_mbed_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ec_ctx) { - return (libssh2_curve_type)ec_ctx->MBEDTLS_PRIVATE(grp).id; + return (ssh2_curve_type)ec_ctx->MBEDTLS_PRIVATE(grp).id; } /* - * returns 0 for success, key curve type that maps to libssh2_curve_type + * returns 0 for success, key curve type that maps to ssh2_curve_type */ -int _libssh2_mbedtls_ecdsa_curve_type_from_name(const char *name, - libssh2_curve_type *out_type) +int ssh2_mbed_ecdsa_curve_type_from_name(const char *name, + ssh2_curve_type *out_type) { int ret = 0; - libssh2_curve_type type; + ssh2_curve_type type; if(!name || strlen(name) != 19) return -1; if(strcmp(name, "ecdsa-sha2-nistp256") == 0) - type = LIBSSH2_EC_CURVE_NISTP256; + type = SSH2_EC_CURVE_NISTP256; else if(strcmp(name, "ecdsa-sha2-nistp384") == 0) - type = LIBSSH2_EC_CURVE_NISTP384; + type = SSH2_EC_CURVE_NISTP384; else if(strcmp(name, "ecdsa-sha2-nistp521") == 0) - type = LIBSSH2_EC_CURVE_NISTP521; + type = SSH2_EC_CURVE_NISTP521; else { ret = -1; } @@ -1405,7 +1389,7 @@ int _libssh2_mbedtls_ecdsa_curve_type_from_name(const char *name, return ret; } -void _libssh2_mbedtls_ecdsa_free(libssh2_ecdsa_ctx *ctx) +void ssh2_mbed_ecdsa_free(ssh2_ecdsa_ctx *ctx) { mbedtls_ecdsa_free(ctx); mbedtls_free(ctx); @@ -1415,9 +1399,9 @@ void _libssh2_mbedtls_ecdsa_free(libssh2_ecdsa_ctx *ctx) /* * Return supported key hash algo upgrades, see crypto.h */ -const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session, - unsigned char *key_method, - size_t key_method_len) +const char *ssh2_supported_key_sign_algs(LIBSSH2_SESSION *session, + unsigned char *key_method, + size_t key_method_len) { (void)session; diff --git a/src/mbedtls.h b/src/mbedtls.h index f2e7f687..62c87891 100644 --- a/src/mbedtls.h +++ b/src/mbedtls.h @@ -39,7 +39,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -#define LIBSSH2_CRYPTO_ENGINE libssh2_mbedtls +#define SSH2_CRYPTO_ENGINE libssh2_mbedtls #include #include @@ -110,83 +110,83 @@ * mbedTLS backend: Generic functions */ -#define libssh2_crypto_init() _libssh2_mbedtls_init() -#define libssh2_crypto_exit() _libssh2_mbedtls_free() +#define ssh2_crypto_init() ssh2_mbed_init() +#define ssh2_crypto_exit() ssh2_mbed_free() -#define _libssh2_random(buf, len) _libssh2_mbedtls_random(buf, len) +#define ssh2_random(buf, len) ssh2_mbed_random(buf, len) -#define libssh2_prepare_iovec(vec, len) /* Empty. */ +#define ssh2_prepare_iovec(vec, len) /* Empty. */ /*******************************************************************/ /* * mbedTLS backend: HMAC functions */ -#define libssh2_hmac_ctx mbedtls_md_context_t +#define ssh2_hmac_ctx mbedtls_md_context_t /*******************************************************************/ /* * mbedTLS backend: SHA1 functions */ -#define libssh2_sha1_ctx mbedtls_md_context_t +#define ssh2_sha1_ctx mbedtls_md_context_t -#define libssh2_sha1_init(pctx) \ - _libssh2_mbedtls_hash_init(pctx, MBEDTLS_MD_SHA1, NULL, 0) -#define libssh2_sha1_update(ctx, data, datalen) \ +#define ssh2_sha1_init(pctx) \ + ssh2_mbed_hash_init(pctx, MBEDTLS_MD_SHA1, NULL, 0) +#define ssh2_sha1_update(ctx, data, datalen) \ (mbedtls_md_update(&(ctx), (const unsigned char *)(data), datalen) == 0) -#define libssh2_sha1_final(ctx, hash) \ - _libssh2_mbedtls_hash_final(&(ctx), hash) -#define libssh2_sha1(data, datalen, hash) \ - _libssh2_mbedtls_hash(data, datalen, MBEDTLS_MD_SHA1, hash) +#define ssh2_sha1_final(ctx, hash) \ + ssh2_mbed_hash_final(&(ctx), hash) +#define ssh2_sha1(data, datalen, hash) \ + ssh2_mbed_hash(data, datalen, MBEDTLS_MD_SHA1, hash) /*******************************************************************/ /* * mbedTLS backend: SHA256 functions */ -#define libssh2_sha256_ctx mbedtls_md_context_t +#define ssh2_sha256_ctx mbedtls_md_context_t -#define libssh2_sha256_init(pctx) \ - _libssh2_mbedtls_hash_init(pctx, MBEDTLS_MD_SHA256, NULL, 0) -#define libssh2_sha256_update(ctx, data, datalen) \ +#define ssh2_sha256_init(pctx) \ + ssh2_mbed_hash_init(pctx, MBEDTLS_MD_SHA256, NULL, 0) +#define ssh2_sha256_update(ctx, data, datalen) \ (mbedtls_md_update(&(ctx), (const unsigned char *)(data), datalen) == 0) -#define libssh2_sha256_final(ctx, hash) \ - _libssh2_mbedtls_hash_final(&(ctx), hash) -#define libssh2_sha256(data, datalen, hash) \ - _libssh2_mbedtls_hash(data, datalen, MBEDTLS_MD_SHA256, hash) +#define ssh2_sha256_final(ctx, hash) \ + ssh2_mbed_hash_final(&(ctx), hash) +#define ssh2_sha256(data, datalen, hash) \ + ssh2_mbed_hash(data, datalen, MBEDTLS_MD_SHA256, hash) /*******************************************************************/ /* * mbedTLS backend: SHA384 functions */ -#define libssh2_sha384_ctx mbedtls_md_context_t +#define ssh2_sha384_ctx mbedtls_md_context_t -#define libssh2_sha384_init(pctx) \ - _libssh2_mbedtls_hash_init(pctx, MBEDTLS_MD_SHA384, NULL, 0) -#define libssh2_sha384_update(ctx, data, datalen) \ +#define ssh2_sha384_init(pctx) \ + ssh2_mbed_hash_init(pctx, MBEDTLS_MD_SHA384, NULL, 0) +#define ssh2_sha384_update(ctx, data, datalen) \ (mbedtls_md_update(&(ctx), (const unsigned char *)(data), datalen) == 0) -#define libssh2_sha384_final(ctx, hash) \ - _libssh2_mbedtls_hash_final(&(ctx), hash) -#define libssh2_sha384(data, datalen, hash) \ - _libssh2_mbedtls_hash(data, datalen, MBEDTLS_MD_SHA384, hash) +#define ssh2_sha384_final(ctx, hash) \ + ssh2_mbed_hash_final(&(ctx), hash) +#define ssh2_sha384(data, datalen, hash) \ + ssh2_mbed_hash(data, datalen, MBEDTLS_MD_SHA384, hash) /*******************************************************************/ /* * mbedTLS backend: SHA512 functions */ -#define libssh2_sha512_ctx mbedtls_md_context_t +#define ssh2_sha512_ctx mbedtls_md_context_t -#define libssh2_sha512_init(pctx) \ - _libssh2_mbedtls_hash_init(pctx, MBEDTLS_MD_SHA512, NULL, 0) -#define libssh2_sha512_update(ctx, data, datalen) \ +#define ssh2_sha512_init(pctx) \ + ssh2_mbed_hash_init(pctx, MBEDTLS_MD_SHA512, NULL, 0) +#define ssh2_sha512_update(ctx, data, datalen) \ (mbedtls_md_update(&(ctx), (const unsigned char *)(data), datalen) == 0) -#define libssh2_sha512_final(ctx, hash) \ - _libssh2_mbedtls_hash_final(&(ctx), hash) -#define libssh2_sha512(data, datalen, hash) \ - _libssh2_mbedtls_hash(data, datalen, MBEDTLS_MD_SHA512, hash) +#define ssh2_sha512_final(ctx, hash) \ + ssh2_mbed_hash_final(&(ctx), hash) +#define ssh2_sha512(data, datalen, hash) \ + ssh2_mbed_hash(data, datalen, MBEDTLS_MD_SHA512, hash) /*******************************************************************/ /* @@ -194,14 +194,14 @@ */ #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM -#define libssh2_md5_ctx mbedtls_md_context_t +#define ssh2_md5_ctx mbedtls_md_context_t -#define libssh2_md5_init(pctx) \ - _libssh2_mbedtls_hash_init(pctx, MBEDTLS_MD_MD5, NULL, 0) -#define libssh2_md5_update(ctx, data, datalen) \ +#define ssh2_md5_init(pctx) \ + ssh2_mbed_hash_init(pctx, MBEDTLS_MD_MD5, NULL, 0) +#define ssh2_md5_update(ctx, data, datalen) \ (mbedtls_md_update(&(ctx), (const unsigned char *)(data), datalen) == 0) -#define libssh2_md5_final(ctx, hash) \ - _libssh2_mbedtls_hash_final(&(ctx), hash) +#define ssh2_md5_final(ctx, hash) \ + ssh2_mbed_hash_final(&(ctx), hash) #endif /*******************************************************************/ @@ -209,37 +209,29 @@ * mbedTLS backend: RSA functions */ -#define libssh2_rsa_ctx mbedtls_rsa_context - -#define _libssh2_rsa_new(rsactx, e, e_len, n, n_len, \ - d, d_len, p, p_len, q, q_len, \ - e1, e1_len, e2, e2_len, c, c_len) \ - _libssh2_mbedtls_rsa_new(rsactx, e, e_len, n, n_len, \ - d, d_len, p, p_len, q, q_len, \ - e1, e1_len, e2, e2_len, c, c_len) - -#define _libssh2_rsa_new_private(rsactx, s, filename, passphrase) \ - _libssh2_mbedtls_rsa_new_private(rsactx, s, filename, passphrase) - -#define _libssh2_rsa_new_private_frommemory(rsactx, s, filedata, \ - filedata_len, passphrase) \ - _libssh2_mbedtls_rsa_new_private_frommemory(rsactx, s, filedata, \ - filedata_len, passphrase) - -#define _libssh2_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) \ - _libssh2_mbedtls_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) - -#define _libssh2_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) \ - _libssh2_mbedtls_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) - -#define _libssh2_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) \ - _libssh2_mbedtls_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) - -#define _libssh2_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) \ - _libssh2_mbedtls_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) - -#define _libssh2_rsa_free(rsactx) \ - _libssh2_mbedtls_rsa_free(rsactx) +#define ssh2_rsa_ctx mbedtls_rsa_context +#define ssh2_rsa_new(rsactx, e, e_len, n, n_len, \ + d, d_len, p, p_len, q, q_len, \ + e1, e1_len, e2, e2_len, c, c_len) \ + ssh2_mbed_rsa_new(rsactx, e, e_len, n, n_len, \ + d, d_len, p, p_len, q, q_len, \ + e1, e1_len, e2, e2_len, c, c_len) +#define ssh2_rsa_new_private(rsactx, s, filename, passphrase) \ + ssh2_mbed_rsa_new_private(rsactx, s, filename, passphrase) +#define ssh2_rsa_new_private_frommemory(rsactx, s, filedata, \ + filedata_len, passphrase) \ + ssh2_mbed_rsa_new_private_frommemory(rsactx, s, filedata, \ + filedata_len, passphrase) +#define ssh2_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) \ + ssh2_mbed_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) +#define ssh2_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) \ + ssh2_mbed_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) +#define ssh2_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) \ + ssh2_mbed_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) +#define ssh2_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) \ + ssh2_mbed_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) +#define ssh2_rsa_free(rsactx) \ + ssh2_mbed_rsa_free(rsactx) /*******************************************************************/ /* @@ -250,25 +242,25 @@ typedef enum { #ifdef MBEDTLS_ECP_DP_SECP256R1_ENABLED - LIBSSH2_EC_CURVE_NISTP256 = MBEDTLS_ECP_DP_SECP256R1, + SSH2_EC_CURVE_NISTP256 = MBEDTLS_ECP_DP_SECP256R1, #else - LIBSSH2_EC_CURVE_NISTP256 = MBEDTLS_ECP_DP_NONE, + SSH2_EC_CURVE_NISTP256 = MBEDTLS_ECP_DP_NONE, #endif #ifdef MBEDTLS_ECP_DP_SECP384R1_ENABLED - LIBSSH2_EC_CURVE_NISTP384 = MBEDTLS_ECP_DP_SECP384R1, + SSH2_EC_CURVE_NISTP384 = MBEDTLS_ECP_DP_SECP384R1, #else - LIBSSH2_EC_CURVE_NISTP384 = MBEDTLS_ECP_DP_NONE, + SSH2_EC_CURVE_NISTP384 = MBEDTLS_ECP_DP_NONE, #endif #ifdef MBEDTLS_ECP_DP_SECP521R1_ENABLED - LIBSSH2_EC_CURVE_NISTP521 = MBEDTLS_ECP_DP_SECP521R1 + SSH2_EC_CURVE_NISTP521 = MBEDTLS_ECP_DP_SECP521R1 #else - LIBSSH2_EC_CURVE_NISTP521 = MBEDTLS_ECP_DP_NONE, + SSH2_EC_CURVE_NISTP521 = MBEDTLS_ECP_DP_NONE, #endif -} libssh2_curve_type; +} ssh2_curve_type; -# define libssh2_ec_key mbedtls_ecp_keypair +# define ssh2_ec_key mbedtls_ecp_keypair #else -# define libssh2_ec_key void +# define ssh2_ec_key void #endif /* LIBSSH2_ECDSA */ /*******************************************************************/ @@ -278,38 +270,29 @@ typedef enum { #if LIBSSH2_ECDSA -#define libssh2_ecdsa_ctx mbedtls_ecdsa_context - -#define _libssh2_ecdsa_create_key(session, privkey, pubkey_octal, \ - pubkey_octal_len, curve) \ - _libssh2_mbedtls_ecdsa_create_key(session, privkey, pubkey_octal, \ - pubkey_octal_len, curve) - -#define _libssh2_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) \ - _libssh2_mbedtls_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) - -#define _libssh2_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) \ - _libssh2_mbedtls_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) - -#define _libssh2_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) \ - _libssh2_mbedtls_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) - -#define _libssh2_ecdsa_new_private(ctx, session, filename, passphrase) \ - _libssh2_mbedtls_ecdsa_new_private(ctx, session, filename, passphrase) - -#define _libssh2_ecdsa_new_private_frommemory(ctx, session, filedata, \ - filedata_len, passphrase) \ - _libssh2_mbedtls_ecdsa_new_private_frommemory(ctx, session, filedata, \ - filedata_len, passphrase) - -#define _libssh2_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) \ - _libssh2_mbedtls_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) - -#define _libssh2_ecdsa_get_curve_type(ctx) \ - _libssh2_mbedtls_ecdsa_get_curve_type(ctx) - -#define _libssh2_ecdsa_free(ctx) \ - _libssh2_mbedtls_ecdsa_free(ctx) +#define ssh2_ecdsa_ctx mbedtls_ecdsa_context +#define ssh2_ecdsa_create_key(session, privkey, pubkey_octal, \ + pubkey_octal_len, curve) \ + ssh2_mbed_ecdsa_create_key(session, privkey, pubkey_octal, \ + pubkey_octal_len, curve) +#define ssh2_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) \ + ssh2_mbed_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) +#define ssh2_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) \ + ssh2_mbed_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) +#define ssh2_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) \ + ssh2_mbed_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) +#define ssh2_ecdsa_new_private(ctx, session, filename, passphrase) \ + ssh2_mbed_ecdsa_new_private(ctx, session, filename, passphrase) +#define ssh2_ecdsa_new_private_frommemory(ctx, session, filedata, \ + filedata_len, passphrase) \ + ssh2_mbed_ecdsa_new_private_frommemory(ctx, session, filedata, \ + filedata_len, passphrase) +#define ssh2_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) \ + ssh2_mbed_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) +#define ssh2_ecdsa_get_curve_type(ctx) \ + ssh2_mbed_ecdsa_get_curve_type(ctx) +#define ssh2_ecdsa_free(ctx) \ + ssh2_mbed_ecdsa_free(ctx) #endif /* LIBSSH2_ECDSA */ @@ -318,80 +301,78 @@ typedef enum { * mbedTLS backend: Key functions */ -#define _libssh2_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) \ - _libssh2_mbedtls_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) -#define _libssh2_pub_priv_keyfilememory(s, m, m_len, p, p_len, \ - pk, pk_len, pw) \ - _libssh2_mbedtls_pub_priv_keyfilememory(s, m, m_len, p, p_len, \ - pk, pk_len, pw) -#define _libssh2_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ - f, kh, kh_len, pk, pk_len, pw) \ - _libssh2_mbedtls_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ - f, kh, kh_len, pk, pk_len, pw) +#define ssh2_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) \ + ssh2_mbed_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) +#define ssh2_pub_priv_keyfilememory(s, m, m_len, p, p_len, pk, pk_len, pw) \ + ssh2_mbed_pub_priv_keyfilememory(s, m, m_len, p, p_len, pk, pk_len, pw) +#define ssh2_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ + f, kh, kh_len, pk, pk_len, pw) \ + ssh2_mbed_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ + f, kh, kh_len, pk, pk_len, pw) /*******************************************************************/ /* * mbedTLS backend: Cipher Context structure */ -#define libssh2_cipher_ctx mbedtls_cipher_context_t +#define ssh2_cipher_ctx mbedtls_cipher_context_t -#define LIBSSH2_CIPHER_T(algo) mbedtls_cipher_type_t algo +#define SSH2_CIPHER_T(algo) mbedtls_cipher_type_t algo -#define libssh2_cipher_aes256ctr MBEDTLS_CIPHER_AES_256_CTR -#define libssh2_cipher_aes192ctr MBEDTLS_CIPHER_AES_192_CTR -#define libssh2_cipher_aes128ctr MBEDTLS_CIPHER_AES_128_CTR -#define libssh2_cipher_aes256 MBEDTLS_CIPHER_AES_256_CBC -#define libssh2_cipher_aes192 MBEDTLS_CIPHER_AES_192_CBC -#define libssh2_cipher_aes128 MBEDTLS_CIPHER_AES_128_CBC +#define ssh2_cipher_aes256ctr MBEDTLS_CIPHER_AES_256_CTR +#define ssh2_cipher_aes192ctr MBEDTLS_CIPHER_AES_192_CTR +#define ssh2_cipher_aes128ctr MBEDTLS_CIPHER_AES_128_CTR +#define ssh2_cipher_aes256 MBEDTLS_CIPHER_AES_256_CBC +#define ssh2_cipher_aes192 MBEDTLS_CIPHER_AES_192_CBC +#define ssh2_cipher_aes128 MBEDTLS_CIPHER_AES_128_CBC #ifdef MBEDTLS_CIPHER_BLOWFISH_CBC -#define libssh2_cipher_blowfish MBEDTLS_CIPHER_BLOWFISH_CBC +#define ssh2_cipher_blowfish MBEDTLS_CIPHER_BLOWFISH_CBC #endif #ifdef MBEDTLS_CIPHER_ARC4_128 -#define libssh2_cipher_arcfour MBEDTLS_CIPHER_ARC4_128 +#define ssh2_cipher_arcfour MBEDTLS_CIPHER_ARC4_128 #endif -#define libssh2_cipher_3des MBEDTLS_CIPHER_DES_EDE3_CBC -#define libssh2_cipher_chacha20 MBEDTLS_CIPHER_CHACHA20_POLY1305 +#define ssh2_cipher_3des MBEDTLS_CIPHER_DES_EDE3_CBC +#define ssh2_cipher_chacha20 MBEDTLS_CIPHER_CHACHA20_POLY1305 /*******************************************************************/ /* * mbedTLS backend: Cipher functions */ -#define _libssh2_cipher_init(ctx, type, iv, secret, encrypt) \ - _libssh2_mbedtls_cipher_init(ctx, type, iv, secret, encrypt) -#define _libssh2_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) \ - _libssh2_mbedtls_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) -#define _libssh2_cipher_dtor(ctx) \ - _libssh2_mbedtls_cipher_dtor(ctx) +#define ssh2_cipher_init(ctx, type, iv, secret, encrypt) \ + ssh2_mbed_cipher_init(ctx, type, iv, secret, encrypt) +#define ssh2_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) \ + ssh2_mbed_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) +#define ssh2_cipher_dtor(ctx) \ + ssh2_mbed_cipher_dtor(ctx) /*******************************************************************/ /* * mbedTLS backend: BigNumber Support */ -#define libssh2_bn_ctx int /* not used */ -#define _libssh2_bn_ctx_new() 0 /* not used */ -#define _libssh2_bn_ctx_free(bnctx) ((void)0) /* not used */ +#define ssh2_bn_ctx int /* not used */ +#define ssh2_bn_ctx_new() 0 /* not used */ +#define ssh2_bn_ctx_free(bnctx) ((void)0) /* not used */ -#define libssh2_bn mbedtls_mpi +#define ssh2_bn mbedtls_mpi -#define _libssh2_bn_init() \ - _libssh2_mbedtls_bignum_init() -#define _libssh2_bn_init_from_bin() \ - _libssh2_mbedtls_bignum_init() -#define _libssh2_bn_set_word(bn, word) \ +#define ssh2_bn_init() \ + ssh2_mbed_bn_init() +#define ssh2_bn_init_from_bin() \ + ssh2_mbed_bn_init() +#define ssh2_bn_set_word(bn, word) \ mbedtls_mpi_lset(bn, word) -#define _libssh2_bn_from_bin(bn, len, bin) \ +#define ssh2_bn_from_bin(bn, len, bin) \ mbedtls_mpi_read_binary(bn, bin, len) -#define _libssh2_bn_to_bin(bn, bin) \ +#define ssh2_bn_to_bin(bn, bin) \ mbedtls_mpi_write_binary(bn, bin, mbedtls_mpi_size(bn)) -#define _libssh2_bn_bytes(bn) \ +#define ssh2_bn_bytes(bn) \ mbedtls_mpi_size(bn) -#define _libssh2_bn_bits(bn) \ +#define ssh2_bn_bits(bn) \ mbedtls_mpi_bitlen(bn) -#define _libssh2_bn_free(bn) \ - _libssh2_mbedtls_bignum_free(bn) +#define ssh2_bn_free(bn) \ + ssh2_mbed_bn_free(bn) /*******************************************************************/ /* @@ -400,62 +381,56 @@ typedef enum { /* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */ -#define LIBSSH2_DH_GEX_MINGROUP 2048 -#define LIBSSH2_DH_GEX_OPTGROUP 4096 -#define LIBSSH2_DH_GEX_MAXGROUP 8192 +#define SSH2_DH_GEX_MINGROUP 2048 +#define SSH2_DH_GEX_OPTGROUP 4096 +#define SSH2_DH_GEX_MAXGROUP 8192 -#define LIBSSH2_DH_MAX_MODULUS_BITS 16384 +#define SSH2_DH_MAX_MODULUS_BITS 16384 -#define libssh2_dh_ctx mbedtls_mpi * -#define libssh2_dh_init(dhctx) _libssh2_dh_init(dhctx) -#define libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ - _libssh2_dh_key_pair(dhctx, public, g, p, group_order) -#define libssh2_dh_secret(dhctx, secret, f, p, bnctx) \ - _libssh2_dh_secret(dhctx, secret, f, p) -#define libssh2_dh_dtor(dhctx) _libssh2_dh_dtor(dhctx) +#define ssh2_dh_ctx mbedtls_mpi * +#define ssh2_dh_init(dhctx) ssh2_mbed_dh_init(dhctx) +#define ssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ + ssh2_mbed_dh_key_pair(dhctx, public, g, p, group_order) +#define ssh2_dh_secret(dhctx, secret, f, p, bnctx) \ + ssh2_mbed_dh_secret(dhctx, secret, f, p) +#define ssh2_dh_dtor(dhctx) ssh2_mbed_dh_dtor(dhctx) /*******************************************************************/ /* * mbedTLS backend: forward declarations */ -void _libssh2_mbedtls_init(void); +void ssh2_mbed_init(void); +void ssh2_mbed_free(void); -void _libssh2_mbedtls_free(void); +int ssh2_mbed_random(unsigned char *buf, size_t len); -int _libssh2_mbedtls_random(unsigned char *buf, size_t len); +void ssh2_mbed_cipher_dtor(ssh2_cipher_ctx *ctx); -void _libssh2_mbedtls_cipher_dtor(libssh2_cipher_ctx *ctx); +int ssh2_mbed_hash_init(mbedtls_md_context_t *ctx, + mbedtls_md_type_t mdtype, + const unsigned char *key, size_t keylen); +int ssh2_mbed_hash_final(mbedtls_md_context_t *ctx, unsigned char *hash); +int ssh2_mbed_hash(const unsigned char *data, size_t datalen, + mbedtls_md_type_t mdtype, unsigned char *hash); -int _libssh2_mbedtls_hash_init(mbedtls_md_context_t *ctx, - mbedtls_md_type_t mdtype, - const unsigned char *key, size_t keylen); +ssh2_bn *ssh2_mbed_bn_init(void); +void ssh2_mbed_bn_free(ssh2_bn *bn); -int _libssh2_mbedtls_hash_final(mbedtls_md_context_t *ctx, - unsigned char *hash); -int _libssh2_mbedtls_hash(const unsigned char *data, size_t datalen, - mbedtls_md_type_t mdtype, unsigned char *hash); - -libssh2_bn *_libssh2_mbedtls_bignum_init(void); - -void _libssh2_mbedtls_bignum_free(libssh2_bn *bn); - -void _libssh2_mbedtls_rsa_free(libssh2_rsa_ctx *ctx); +void ssh2_mbed_rsa_free(ssh2_rsa_ctx *ctx); #if LIBSSH2_ECDSA -libssh2_curve_type _libssh2_mbedtls_ecdsa_key_get_curve_type( - libssh2_ecdsa_ctx *ctx); -int _libssh2_mbedtls_ecdsa_curve_type_from_name(const char *name, - libssh2_curve_type *out_type); -void _libssh2_mbedtls_ecdsa_free(libssh2_ecdsa_ctx *ctx); +ssh2_curve_type ssh2_mbed_ecdsa_key_get_curve_type(ssh2_ecdsa_ctx *ctx); +int ssh2_mbed_ecdsa_curve_type_from_name(const char *name, + ssh2_curve_type *out_type); +void ssh2_mbed_ecdsa_free(ssh2_ecdsa_ctx *ctx); #endif /* LIBSSH2_ECDSA */ -void _libssh2_init_aes_ctr(void); -void _libssh2_dh_init(libssh2_dh_ctx *dhctx); -int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, int group_order); -int _libssh2_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p); -void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx); +void ssh2_mbed_dh_init(ssh2_dh_ctx *dhctx); +int ssh2_mbed_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, int group_order); +int ssh2_mbed_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p); +void ssh2_mbed_dh_dtor(ssh2_dh_ctx *dhctx); #endif /* LIBSSH2_MBEDTLS_H */ diff --git a/src/misc.c b/src/misc.c index b3c41ccb..19568b62 100644 --- a/src/misc.c +++ b/src/misc.c @@ -50,11 +50,11 @@ #ifdef _WIN32 /* Force parameter type. */ -#define libssh2_recv(s, b, l, f) recv(s, b, (int)(l), f) -#define libssh2_send(s, b, l, f) send(s, b, (int)(l), f) +#define SSH2_RECV_LOW(s, b, l, f) recv(s, b, (int)(l), f) +#define SSH2_SEND_LOW(s, b, l, f) send(s, b, (int)(l), f) #else -#define libssh2_recv recv -#define libssh2_send send +#define SSH2_RECV_LOW recv +#define SSH2_SEND_LOW send #endif /* snprintf not in Visual Studio CRT and _snprintf dangerously incompatible. @@ -67,7 +67,7 @@ * number of chars placed in cp excluding the trailing null char. For * cp_max_len > 0 the return value is always < cp_max_len; for cp_max_len * <= 0 the return value is 0 (and no chars are written to cp). */ -int _libssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...) +int ssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...) { va_list args; int n; @@ -81,8 +81,8 @@ int _libssh2_snprintf(char *cp, size_t cp_max_len, const char *fmt, ...) } #endif -int _libssh2_error_flags(LIBSSH2_SESSION *session, int errcode, - const char *errmsg, int errflags) +int ssh2_err_flags(LIBSSH2_SESSION *session, int errcode, + const char *errmsg, int errflags) { if(!session) { if(errmsg) @@ -90,18 +90,18 @@ int _libssh2_error_flags(LIBSSH2_SESSION *session, int errcode, return errcode; } - if(session->err_flags & LIBSSH2_ERR_FLAG_DUP) - LIBSSH2_FREE(session, (char *)LIBSSH2_UNCONST(session->err_msg)); + if(session->err_flags & SSH2_ERR_FLAG_DUP) + SSH2_FREE(session, (char *)SSH2_UNCONST(session->err_msg)); session->err_code = errcode; session->err_flags = 0; - if(errmsg && ((errflags & LIBSSH2_ERR_FLAG_DUP) != 0)) { + if(errmsg && ((errflags & SSH2_ERR_FLAG_DUP) != 0)) { size_t len = strlen(errmsg); - char *copy = LIBSSH2_ALLOC(session, len + 1); + char *copy = SSH2_ALLOC(session, len + 1); if(copy) { memcpy(copy, errmsg, len + 1); - session->err_flags = LIBSSH2_ERR_FLAG_DUP; + session->err_flags = SSH2_ERR_FLAG_DUP; session->err_msg = copy; } else @@ -116,20 +116,20 @@ int _libssh2_error_flags(LIBSSH2_SESSION *session, int errcode, /* if this is EAGAIN and we are in non-blocking mode, do not generate a debug output for this */ return errcode; - _libssh2_debug((session, LIBSSH2_TRACE_ERROR, "%d - %s", session->err_code, - session->err_msg)); + ssh2_deb((session, LIBSSH2_TRACE_ERROR, "%d - %s", session->err_code, + session->err_msg)); #endif return errcode; } -int _libssh2_error(LIBSSH2_SESSION *session, int errcode, const char *errmsg) +int ssh2_err(LIBSSH2_SESSION *session, int errcode, const char *errmsg) { - return _libssh2_error_flags(session, errcode, errmsg, 0); + return ssh2_err_flags(session, errcode, errmsg, 0); } #ifdef _WIN32 -int _libssh2_wsa2errno(void) +int ssh2_wsa2errno(void) { switch(WSAGetLastError()) { case WSAEWOULDBLOCK: @@ -152,18 +152,18 @@ int _libssh2_wsa2errno(void) /* * Replacement for the standard recv, return -errno on failure. */ -ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer, size_t length, - int flags, void **abstract) +ssize_t ssh2_recv(libssh2_socket_t socket, void *buffer, size_t length, + int flags, void **abstract) { ssize_t rc; (void)abstract; - rc = libssh2_recv(socket, buffer, length, flags); + rc = SSH2_RECV_LOW(socket, buffer, length, flags); if(rc < 0) { int err; #ifdef _WIN32 - err = _libssh2_wsa2errno(); + err = ssh2_wsa2errno(); #else err = errno; #endif @@ -189,19 +189,19 @@ ssize_t _libssh2_recv(libssh2_socket_t socket, void *buffer, size_t length, /* * Replacement for the standard send, return -errno on failure. */ -ssize_t _libssh2_send(libssh2_socket_t socket, - const void *buffer, size_t length, - int flags, void **abstract) +ssize_t ssh2_send(libssh2_socket_t socket, + const void *buffer, size_t length, + int flags, void **abstract) { ssize_t rc; (void)abstract; - rc = libssh2_send(socket, buffer, length, flags); + rc = SSH2_SEND_LOW(socket, buffer, length, flags); if(rc < 0) { int err; #ifdef _WIN32 - err = _libssh2_wsa2errno(); + err = ssh2_wsa2errno(); #else err = errno; #endif @@ -219,7 +219,7 @@ ssize_t _libssh2_send(libssh2_socket_t socket, return rc; } -uint32_t _libssh2_ntohu32(const unsigned char *buf) +uint32_t ssh2_ntohu32(const unsigned char *buf) { return ((uint32_t)buf[0] << 24) | @@ -228,7 +228,7 @@ uint32_t _libssh2_ntohu32(const unsigned char *buf) ((uint32_t)buf[3]); } -libssh2_uint64_t _libssh2_ntohu64(const unsigned char *buf) +libssh2_uint64_t ssh2_ntohu64(const unsigned char *buf) { return ((libssh2_uint64_t)buf[0] << 56) | @@ -241,7 +241,7 @@ libssh2_uint64_t _libssh2_ntohu64(const unsigned char *buf) ((libssh2_uint64_t)buf[7]); } -void _libssh2_htonu32(unsigned char *buf, uint32_t value) +void ssh2_htonu32(unsigned char *buf, uint32_t value) { buf[0] = (unsigned char)((value >> 24) & 0xFF); buf[1] = (value >> 16) & 0xFF; @@ -249,13 +249,13 @@ void _libssh2_htonu32(unsigned char *buf, uint32_t value) buf[3] = value & 0xFF; } -void _libssh2_store_u32(unsigned char **buf, uint32_t value) +void ssh2_store_u32(unsigned char **buf, uint32_t value) { - _libssh2_htonu32(*buf, value); + ssh2_htonu32(*buf, value); *buf += sizeof(uint32_t); } -void _libssh2_store_u64(unsigned char **buf, libssh2_uint64_t value) +void ssh2_store_u64(unsigned char **buf, libssh2_uint64_t value) { unsigned char *ptr = *buf; @@ -271,11 +271,11 @@ void _libssh2_store_u64(unsigned char **buf, libssh2_uint64_t value) *buf += sizeof(libssh2_uint64_t); } -int _libssh2_store_str(unsigned char **buf, const char *str, size_t len) +int ssh2_store_str(unsigned char **buf, const char *str, size_t len) { uint32_t len_stored = (uint32_t)len; - _libssh2_store_u32(buf, len_stored); + ssh2_store_u32(buf, len_stored); if(len_stored) { memcpy(*buf, str, len_stored); *buf += len_stored; @@ -285,8 +285,8 @@ int _libssh2_store_str(unsigned char **buf, const char *str, size_t len) return len_stored == len; } -int _libssh2_store_hybrid_str(unsigned char **buf, const char *str_1, - size_t len_1, const char *str_2, size_t len_2) +int ssh2_store_hybrid_str(unsigned char **buf, const char *str_1, + size_t len_1, const char *str_2, size_t len_2) { uint32_t len_stored; @@ -295,7 +295,7 @@ int _libssh2_store_hybrid_str(unsigned char **buf, const char *str_1, len_stored = (uint32_t)len_1 + (uint32_t)len_2; - _libssh2_store_u32(buf, len_stored); + ssh2_store_u32(buf, len_stored); if(len_1) { memcpy(*buf, str_1, len_1); *buf += len_1; @@ -310,9 +310,8 @@ int _libssh2_store_hybrid_str(unsigned char **buf, const char *str_1, return len_stored == len_1 + len_2; } -int _libssh2_store_bignum2_bytes(unsigned char **buf, - const unsigned char *bytes, - size_t len) +int ssh2_store_bignum_bytes(unsigned char **buf, + const unsigned char *bytes, size_t len) { uint32_t len_stored; uint32_t extraByte; @@ -325,7 +324,7 @@ int _libssh2_store_bignum2_bytes(unsigned char **buf, len_stored = (uint32_t)len; if(extraByte && len_stored == UINT32_MAX) len_stored--; - _libssh2_store_u32(buf, len_stored + extraByte); + ssh2_store_u32(buf, len_stored + extraByte); if(extraByte) { *buf[0] = 0; @@ -374,7 +373,7 @@ int libssh2_base64_decode(LIBSSH2_SESSION *session, int rc; size_t dlen; - rc = _libssh2_base64_decode(session, dest, &dlen, src, src_len); + rc = ssh2_base64_decode(session, dest, &dlen, src, src_len); if(dest_len) *dest_len = (unsigned int)dlen; @@ -386,9 +385,9 @@ int libssh2_base64_decode(LIBSSH2_SESSION *session, /* * Decode a base64 chunk and store it into a newly alloc'd buffer */ -int _libssh2_base64_decode(LIBSSH2_SESSION *session, - char **data, size_t *datalen, - const char *src, size_t src_len) +int ssh2_base64_decode(LIBSSH2_SESSION *session, + char **data, size_t *datalen, + const char *src, size_t src_len) { unsigned char *d; const char *s; @@ -396,11 +395,11 @@ int _libssh2_base64_decode(LIBSSH2_SESSION *session, size_t i = 0, len = 0; *datalen = 0; - *data = LIBSSH2_ALLOC(session, src_len); + *data = SSH2_ALLOC(session, src_len); d = (unsigned char *)*data; if(!d) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for base64 decoding"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for base64 decoding"); } for(s = src; s < (src + src_len); s++) { @@ -428,9 +427,9 @@ int _libssh2_base64_decode(LIBSSH2_SESSION *session, if((i % 4) == 1) { /* Invalid -- We have a byte which belongs exclusively to a partial octet */ - LIBSSH2_FREE(session, *data); + SSH2_FREE(session, *data); *data = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, "Invalid base64"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, "Invalid base64"); } *datalen = len; @@ -442,15 +441,15 @@ static const char table64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /* - * _libssh2_base64_encode + * ssh2_base64_encode() * * Returns the length of the newly created base64 string. The third argument * is a pointer to an allocated area holding the base64 data. If something * went wrong, 0 is returned. * */ -size_t _libssh2_base64_encode(LIBSSH2_SESSION *session, - const char *inp, size_t insize, char **outptr) +size_t ssh2_base64_encode(LIBSSH2_SESSION *session, + const char *inp, size_t insize, char **outptr) { unsigned char ibuf[3]; unsigned char obuf[4]; @@ -466,7 +465,7 @@ size_t _libssh2_base64_encode(LIBSSH2_SESSION *session, if(insize == 0) return 0; /* nothing to encode */ - base64data = output = LIBSSH2_ALLOC(session, insize * 4 / 3 + 4); + base64data = output = SSH2_ALLOC(session, insize * 4 / 3 + 4); if(!output) return 0; @@ -521,7 +520,7 @@ size_t _libssh2_base64_encode(LIBSSH2_SESSION *session, LIBSSH2_API void libssh2_free(LIBSSH2_SESSION *session, void *ptr) { - LIBSSH2_FREE(session, ptr); + SSH2_FREE(session, ptr); } #ifdef LIBSSH2DEBUG @@ -543,8 +542,8 @@ int libssh2_trace_sethandler(LIBSSH2_SESSION *session, void *context, return 0; } -void _libssh2_debug_low(LIBSSH2_SESSION *session, int context, - const char *format, ...) +void ssh2_deb_low(LIBSSH2_SESSION *session, int context, + const char *format, ...) { char buffer[1536]; int len, msglen, buflen = sizeof(buffer); @@ -572,7 +571,7 @@ void _libssh2_debug_low(LIBSSH2_SESSION *session, int context, } /* Find the first matching context string for this message */ - for(contextindex = 0; contextindex < ARRAY_SIZE(contexts); + for(contextindex = 0; contextindex < SSH2_ARRAYSIZE(contexts); contextindex++) { if((context & (1 << contextindex)) != 0) { contexttext = contexts[contextindex]; @@ -635,14 +634,13 @@ int libssh2_trace_sethandler(LIBSSH2_SESSION *session, void *context, #endif /* init the list head */ -void _libssh2_list_init(struct list_head *head) +void ssh2_list_init(struct list_head *head) { head->first = head->last = NULL; } /* add a node to the list */ -void _libssh2_list_add(struct list_head *head, - struct list_node *entry) +void ssh2_list_add(struct list_head *head, struct list_node *entry) { /* store a pointer to the head */ entry->head = head; @@ -664,25 +662,25 @@ void _libssh2_list_add(struct list_head *head, } /* return the "first" node in the list this head points to */ -void *_libssh2_list_first(struct list_head *head) +void *ssh2_list_first(struct list_head *head) { return head->first; } /* return the next node in the list */ -void *_libssh2_list_next(struct list_node *node) +void *ssh2_list_next(struct list_node *node) { return node->next; } /* return the prev node in the list */ -void *_libssh2_list_prev(struct list_node *node) +void *ssh2_list_prev(struct list_node *node) { return node->prev; } /* remove this node from the list */ -void _libssh2_list_remove(struct list_node *entry) +void ssh2_list_remove(struct list_node *entry) { if(entry->prev) entry->prev->next = entry->next; @@ -697,7 +695,7 @@ void _libssh2_list_remove(struct list_node *entry) #if 0 /* insert a node before the given 'after' entry */ -void _libssh2_list_insert(struct list_node *after, /* insert before this */ +void ssh2_list_insert(struct list_node *after, /* insert before this */ struct list_node *entry) { /* 'after' is next to 'entry' */ @@ -744,7 +742,7 @@ void _libssh2_list_insert(struct list_node *after, /* insert before this */ * DISCLAIMED. This includes but is not limited to warranties of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ -int _libssh2_gettimeofday(struct timeval *tp, void *tzp) +int ssh2_gettimeofday(struct timeval *tp, void *tzp) { (void)tzp; if(tp) { @@ -771,9 +769,9 @@ int _libssh2_gettimeofday(struct timeval *tp, void *tzp) } #endif -void *_libssh2_calloc(LIBSSH2_SESSION *session, size_t size) +void *ssh2_calloc(LIBSSH2_SESSION *session, size_t size) { - void *p = LIBSSH2_ALLOC(session, size); + void *p = SSH2_ALLOC(session, size); if(p) { memset(p, 0, size); } @@ -782,10 +780,10 @@ void *_libssh2_calloc(LIBSSH2_SESSION *session, size_t size) /* XOR operation on buffers input1 and input2, result in output. It is safe to use an input buffer as the output buffer. */ -void _libssh2_xor_data(unsigned char *output, - const unsigned char *input1, - const unsigned char *input2, - size_t length) +void ssh2_xor_data(unsigned char *output, + const unsigned char *input1, + const unsigned char *input2, + size_t length) { size_t i; @@ -796,7 +794,7 @@ void _libssh2_xor_data(unsigned char *output, #ifdef LIBSSH2_MEMZERO static void *(* const volatile memset_libssh)(void *, int, size_t) = memset; -void _libssh2_memzero(void *buf, size_t size) +void ssh2_memzero(void *buf, size_t size) { memset_libssh(buf, 0, size); } @@ -804,32 +802,32 @@ void _libssh2_memzero(void *buf, size_t size) /* String buffer */ -struct string_buf *_libssh2_string_buf_new(LIBSSH2_SESSION *session) +struct string_buf *ssh2_string_buf_new(LIBSSH2_SESSION *session) { struct string_buf *ret; - ret = _libssh2_calloc(session, sizeof(*ret)); + ret = ssh2_calloc(session, sizeof(*ret)); if(!ret) return NULL; return ret; } -void _libssh2_string_buf_free(LIBSSH2_SESSION *session, struct string_buf *buf) +void ssh2_string_buf_free(LIBSSH2_SESSION *session, struct string_buf *buf) { if(!buf) return; if(buf->data) - LIBSSH2_FREE(session, buf->data); + SSH2_FREE(session, buf->data); - LIBSSH2_FREE(session, buf); + SSH2_FREE(session, buf); buf = NULL; } -int _libssh2_get_byte(struct string_buf *buf, unsigned char *out) +int ssh2_get_byte(struct string_buf *buf, unsigned char *out) { - if(!_libssh2_check_length(buf, 1)) { + if(!ssh2_check_length(buf, 1)) { return -1; } @@ -838,9 +836,9 @@ int _libssh2_get_byte(struct string_buf *buf, unsigned char *out) return 0; } -int _libssh2_get_boolean(struct string_buf *buf, unsigned char *out) +int ssh2_get_boolean(struct string_buf *buf, unsigned char *out) { - if(!_libssh2_check_length(buf, 1)) { + if(!ssh2_check_length(buf, 1)) { return -1; } @@ -849,47 +847,47 @@ int _libssh2_get_boolean(struct string_buf *buf, unsigned char *out) return 0; } -int _libssh2_get_u32(struct string_buf *buf, uint32_t *out) +int ssh2_get_u32(struct string_buf *buf, uint32_t *out) { - if(!_libssh2_check_length(buf, 4)) { + if(!ssh2_check_length(buf, 4)) { return -1; } - *out = _libssh2_ntohu32(buf->dataptr); + *out = ssh2_ntohu32(buf->dataptr); buf->dataptr += 4; return 0; } -int _libssh2_get_u64(struct string_buf *buf, libssh2_uint64_t *out) +int ssh2_get_u64(struct string_buf *buf, libssh2_uint64_t *out) { - if(!_libssh2_check_length(buf, 8)) { + if(!ssh2_check_length(buf, 8)) { return -1; } - *out = _libssh2_ntohu64(buf->dataptr); + *out = ssh2_ntohu64(buf->dataptr); buf->dataptr += 8; return 0; } -int _libssh2_match_string(struct string_buf *buf, const char *match) +int ssh2_match_string(struct string_buf *buf, const char *match) { unsigned char *out; size_t len = 0; - if(_libssh2_get_string(buf, &out, &len) || len != strlen(match) || + if(ssh2_get_string(buf, &out, &len) || len != strlen(match) || strncmp((char *)out, match, strlen(match)) != 0) { return -1; } return 0; } -int _libssh2_get_string(struct string_buf *buf, unsigned char **outbuf, - size_t *outlen) +int ssh2_get_string(struct string_buf *buf, unsigned char **outbuf, + size_t *outlen) { uint32_t data_len; - if(!buf || _libssh2_get_u32(buf, &data_len) != 0) { + if(!buf || ssh2_get_u32(buf, &data_len) != 0) { return -1; } - if(!_libssh2_check_length(buf, data_len)) { + if(!ssh2_check_length(buf, data_len)) { return -1; } *outbuf = buf->dataptr; @@ -901,18 +899,18 @@ int _libssh2_get_string(struct string_buf *buf, unsigned char **outbuf, return 0; } -int _libssh2_copy_string(LIBSSH2_SESSION *session, struct string_buf *buf, - unsigned char **outbuf, size_t *outlen) +int ssh2_copy_string(LIBSSH2_SESSION *session, struct string_buf *buf, + unsigned char **outbuf, size_t *outlen) { size_t str_len; unsigned char *str; - if(_libssh2_get_string(buf, &str, &str_len)) { + if(ssh2_get_string(buf, &str, &str_len)) { return -1; } if(str_len) { - *outbuf = LIBSSH2_ALLOC(session, str_len); + *outbuf = SSH2_ALLOC(session, str_len); if(*outbuf) { memcpy(*outbuf, str, str_len); } @@ -930,17 +928,17 @@ int _libssh2_copy_string(LIBSSH2_SESSION *session, struct string_buf *buf, return 0; } -int _libssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf, - size_t *outlen) +int ssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf, + size_t *outlen) { uint32_t data_len; uint32_t bn_len; unsigned char *bnptr; - if(_libssh2_get_u32(buf, &data_len)) { + if(ssh2_get_u32(buf, &data_len)) { return -1; } - if(!_libssh2_check_length(buf, data_len)) { + if(!ssh2_check_length(buf, data_len)) { return -1; } @@ -962,24 +960,24 @@ int _libssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf, return 0; } -/* Given the current location in buf, _libssh2_check_length ensures +/* Given the current location in buf, ssh2_check_length() ensures callers can read the next len number of bytes out of the buffer before reading the buffer content */ -int _libssh2_check_length(struct string_buf *buf, size_t requested_len) +int ssh2_check_length(struct string_buf *buf, size_t requested_len) { unsigned char *endp = &buf->data[buf->len]; size_t left = endp - buf->dataptr; return (requested_len <= left) && (left <= buf->len); } -int _libssh2_eob(struct string_buf *buf) +int ssh2_eob(struct string_buf *buf) { unsigned char *endp = &buf->data[buf->len]; return buf->dataptr >= endp; } -int _libssh2_timingsafe_bcmp(const void *b1, const void *b2, size_t n) +int ssh2_timingsafe_bcmp(const void *b1, const void *b2, size_t n) { const unsigned char *p1 = (const unsigned char *)b1; const unsigned char *p2 = (const unsigned char *)b2; diff --git a/src/misc.h b/src/misc.h index 8af247e2..0d6a9f3b 100644 --- a/src/misc.h +++ b/src/misc.h @@ -40,23 +40,23 @@ */ #ifdef LIBSSH2_NO_CLEAR_MEMORY -#define _libssh2_explicit_zero(buf, size) \ - do { \ - (void)(buf); \ - (void)(size); \ +#define ssh2_explicit_zero(buf, size) \ + do { \ + (void)(buf); \ + (void)(size); \ } while(0) #elif defined(_WIN32) -#define _libssh2_explicit_zero(buf, size) SecureZeroMemory(buf, size) +#define ssh2_explicit_zero(buf, size) SecureZeroMemory(buf, size) #elif defined(HAVE_EXPLICIT_BZERO) -#define _libssh2_explicit_zero(buf, size) explicit_bzero(buf, size) +#define ssh2_explicit_zero(buf, size) explicit_bzero(buf, size) #elif defined(HAVE_EXPLICIT_MEMSET) -#define _libssh2_explicit_zero(buf, size) (void)explicit_memset(buf, 0, size) +#define ssh2_explicit_zero(buf, size) (void)explicit_memset(buf, 0, size) #elif defined(HAVE_MEMSET_S) -#define _libssh2_explicit_zero(buf, size) (void)memset_s(buf, size, 0, size) +#define ssh2_explicit_zero(buf, size) (void)memset_s(buf, size, 0, size) #else #define LIBSSH2_MEMZERO -void _libssh2_memzero(void *buf, size_t size); -#define _libssh2_explicit_zero(buf, size) _libssh2_memzero(buf, size) +void ssh2_memzero(void *buf, size_t size); +#define ssh2_explicit_zero(buf, size) ssh2_memzero(buf, size) #endif struct list_head { @@ -76,74 +76,70 @@ struct string_buf { size_t len; }; -int _libssh2_error_flags(LIBSSH2_SESSION *session, int errcode, - const char *errmsg, int errflags); -int _libssh2_error(LIBSSH2_SESSION *session, int errcode, const char *errmsg); +int ssh2_err_flags(LIBSSH2_SESSION *session, int errcode, + const char *errmsg, int errflags); +int ssh2_err(LIBSSH2_SESSION *session, int errcode, const char *errmsg); #ifdef _WIN32 /* Convert Win32 WSAGetLastError to errno equivalent */ -int _libssh2_wsa2errno(void); +int ssh2_wsa2errno(void); #endif -void _libssh2_list_init(struct list_head *head); +void ssh2_list_init(struct list_head *head); /* add a node last in the list */ -void _libssh2_list_add(struct list_head *head, - struct list_node *entry); +void ssh2_list_add(struct list_head *head, struct list_node *entry); /* return the "first" node in the list this head points to */ -void *_libssh2_list_first(struct list_head *head); +void *ssh2_list_first(struct list_head *head); /* return the next node in the list */ -void *_libssh2_list_next(struct list_node *node); +void *ssh2_list_next(struct list_node *node); /* return the prev node in the list */ -void *_libssh2_list_prev(struct list_node *node); +void *ssh2_list_prev(struct list_node *node); /* remove this node from the list */ -void _libssh2_list_remove(struct list_node *entry); +void ssh2_list_remove(struct list_node *entry); -int _libssh2_base64_decode(LIBSSH2_SESSION *session, - char **data, size_t *datalen, - const char *src, size_t src_len); -size_t _libssh2_base64_encode(LIBSSH2_SESSION *session, - const char *inp, size_t insize, char **outptr); +int ssh2_base64_decode(LIBSSH2_SESSION *session, char **data, size_t *datalen, + const char *src, size_t src_len); +size_t ssh2_base64_encode(LIBSSH2_SESSION *session, + const char *inp, size_t insize, char **outptr); -uint32_t _libssh2_ntohu32(const unsigned char *buf); -libssh2_uint64_t _libssh2_ntohu64(const unsigned char *buf); -void _libssh2_htonu32(unsigned char *buf, uint32_t value); -void _libssh2_store_u32(unsigned char **buf, uint32_t value); -void _libssh2_store_u64(unsigned char **buf, libssh2_uint64_t value); -int _libssh2_store_str(unsigned char **buf, const char *str, size_t len); -int _libssh2_store_hybrid_str(unsigned char **buf, const char *str_1, - size_t len_1, const char *str_2, size_t len_2); -int _libssh2_store_bignum2_bytes(unsigned char **buf, - const unsigned char *bytes, - size_t len); -void *_libssh2_calloc(LIBSSH2_SESSION *session, size_t size); +uint32_t ssh2_ntohu32(const unsigned char *buf); +libssh2_uint64_t ssh2_ntohu64(const unsigned char *buf); +void ssh2_htonu32(unsigned char *buf, uint32_t value); +void ssh2_store_u32(unsigned char **buf, uint32_t value); +void ssh2_store_u64(unsigned char **buf, libssh2_uint64_t value); +int ssh2_store_str(unsigned char **buf, const char *str, size_t len); +int ssh2_store_hybrid_str(unsigned char **buf, const char *str_1, + size_t len_1, const char *str_2, size_t len_2); +int ssh2_store_bignum_bytes(unsigned char **buf, + const unsigned char *bytes, size_t len); +void *ssh2_calloc(LIBSSH2_SESSION *session, size_t size); -struct string_buf *_libssh2_string_buf_new(LIBSSH2_SESSION *session); -void _libssh2_string_buf_free(LIBSSH2_SESSION *session, - struct string_buf *buf); -int _libssh2_get_boolean(struct string_buf *buf, unsigned char *out); -int _libssh2_get_byte(struct string_buf *buf, unsigned char *out); -int _libssh2_get_u32(struct string_buf *buf, uint32_t *out); -int _libssh2_get_u64(struct string_buf *buf, libssh2_uint64_t *out); -int _libssh2_match_string(struct string_buf *buf, const char *match); -int _libssh2_get_string(struct string_buf *buf, unsigned char **outbuf, - size_t *outlen); -int _libssh2_copy_string(LIBSSH2_SESSION *session, struct string_buf *buf, - unsigned char **outbuf, size_t *outlen); -int _libssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf, - size_t *outlen); -int _libssh2_check_length(struct string_buf *buf, size_t requested_len); -int _libssh2_eob(struct string_buf *buf); +struct string_buf *ssh2_string_buf_new(LIBSSH2_SESSION *session); +void ssh2_string_buf_free(LIBSSH2_SESSION *session, struct string_buf *buf); +int ssh2_get_boolean(struct string_buf *buf, unsigned char *out); +int ssh2_get_byte(struct string_buf *buf, unsigned char *out); +int ssh2_get_u32(struct string_buf *buf, uint32_t *out); +int ssh2_get_u64(struct string_buf *buf, libssh2_uint64_t *out); +int ssh2_match_string(struct string_buf *buf, const char *match); +int ssh2_get_string(struct string_buf *buf, unsigned char **outbuf, + size_t *outlen); +int ssh2_copy_string(LIBSSH2_SESSION *session, struct string_buf *buf, + unsigned char **outbuf, size_t *outlen); +int ssh2_get_bignum_bytes(struct string_buf *buf, unsigned char **outbuf, + size_t *outlen); +int ssh2_check_length(struct string_buf *buf, size_t requested_len); +int ssh2_eob(struct string_buf *buf); -void _libssh2_xor_data(unsigned char *output, - const unsigned char *input1, - const unsigned char *input2, - size_t length); +void ssh2_xor_data(unsigned char *output, + const unsigned char *input1, + const unsigned char *input2, + size_t length); -int _libssh2_timingsafe_bcmp(const void *b1, const void *b2, size_t n); +int ssh2_timingsafe_bcmp(const void *b1, const void *b2, size_t n); #endif /* LIBSSH2_MISC_H */ diff --git a/src/openssl.c b/src/openssl.c index 861d7a23..a0505289 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -50,7 +50,7 @@ #define USE_PEM_READ_BIO_PRIVATEKEY #endif -int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx) +int ssh2_hmac_ctx_init(ssh2_hmac_ctx *ctx) { #ifdef USE_OPENSSL_3 *ctx = NULL; @@ -65,7 +65,7 @@ int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx) } #ifdef USE_OPENSSL_3 -static int hmac_init(libssh2_hmac_ctx *ctx, +static int hmac_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen, const char *digest_name) { @@ -84,7 +84,7 @@ static int hmac_init(libssh2_hmac_ctx *ctx, params[0] = OSSL_PARAM_construct_octet_string( OSSL_MAC_PARAM_KEY, (void *)key, keylen); params[1] = OSSL_PARAM_construct_utf8_string( - OSSL_MAC_PARAM_DIGEST, (char *)LIBSSH2_UNCONST(digest_name), 0); + OSSL_MAC_PARAM_DIGEST, (char *)SSH2_UNCONST(digest_name), 0); params[2] = OSSL_PARAM_construct_end(); return EVP_MAC_init(*ctx, NULL, 0, params); @@ -92,7 +92,7 @@ static int hmac_init(libssh2_hmac_ctx *ctx, #endif #if LIBSSH2_MD5 -int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_md5_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { #ifdef USE_OPENSSL_3 return hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_MD5); @@ -105,8 +105,7 @@ int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) #endif #if LIBSSH2_HMAC_RIPEMD -int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen) +int ssh2_hmac_ripemd160_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { #ifdef USE_OPENSSL_3 return hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_RIPEMD160); @@ -118,7 +117,7 @@ int _libssh2_hmac_ripemd160_init(libssh2_hmac_ctx *ctx, } #endif -int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha1_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { #ifdef USE_OPENSSL_3 return hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_SHA1); @@ -129,7 +128,7 @@ int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) #endif } -int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha256_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { #ifdef USE_OPENSSL_3 return hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_SHA2_256); @@ -140,7 +139,7 @@ int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) #endif } -int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha512_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { #ifdef USE_OPENSSL_3 return hmac_init(ctx, key, keylen, OSSL_DIGEST_NAME_SHA2_512); @@ -151,8 +150,7 @@ int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) #endif } -int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, - const void *data, size_t datalen) +int ssh2_hmac_update(ssh2_hmac_ctx *ctx, const void *data, size_t datalen) { #ifdef USE_OPENSSL_3 return EVP_MAC_update(*ctx, data, datalen); @@ -168,7 +166,7 @@ int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, #endif } -int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data) +int ssh2_hmac_final(ssh2_hmac_ctx *ctx, void *data) { #ifdef USE_OPENSSL_3 return EVP_MAC_final(*ctx, data, NULL, MAX_MACSIZE); @@ -179,7 +177,7 @@ int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data) #endif } -void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) +void ssh2_hmac_cleanup(ssh2_hmac_ctx *ctx) { #ifdef USE_OPENSSL_3 EVP_MAC_CTX_free(*ctx); @@ -231,7 +229,7 @@ static unsigned char *write_bn(unsigned char *buf, const BIGNUM *bn, if(!(*(p + 1) & 0x80)) { memmove(p, p + 1, --bn_bytes); } - _libssh2_htonu32(p - 4, bn_bytes); /* Post write bn size. */ + ssh2_htonu32(p - 4, bn_bytes); /* Post write bn size. */ return p + bn_bytes; } @@ -252,7 +250,7 @@ static inline void swap_bytes(unsigned char *buf, unsigned long len) } #endif -int _libssh2_openssl_random(void *buf, size_t len) +int ssh2_ossl_random(void *buf, size_t len) { if(len > INT_MAX) { return -1; @@ -262,15 +260,15 @@ int _libssh2_openssl_random(void *buf, size_t len) } #if LIBSSH2_RSA -int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, - const unsigned char *edata, unsigned long elen, - const unsigned char *ndata, unsigned long nlen, - const unsigned char *ddata, unsigned long dlen, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *e1data, unsigned long e1len, - const unsigned char *e2data, unsigned long e2len, - const unsigned char *coeffdata, unsigned long coefflen) +int ssh2_rsa_new(ssh2_rsa_ctx **rsa, + const unsigned char *edata, unsigned long elen, + const unsigned char *ndata, unsigned long nlen, + const unsigned char *ddata, unsigned long dlen, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *e1data, unsigned long e1len, + const unsigned char *e2data, unsigned long e2len, + const unsigned char *coeffdata, unsigned long coefflen) { #ifdef USE_OPENSSL_3 int ret = 0; @@ -407,11 +405,11 @@ int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, #endif /* USE_OPENSSL_3 */ } -int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, - size_t hash_len, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len) +int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsactx, + size_t hash_len, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len) { #ifdef USE_OPENSSL_3 EVP_PKEY_CTX *ctx = NULL; @@ -426,15 +424,15 @@ int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, if(hash_len == SHA_DIGEST_LENGTH) { nid_type = NID_sha1; - ret = _libssh2_sha1(m, m_len, hash); + ret = ssh2_ossl_sha1(m, m_len, hash); } else if(hash_len == SHA256_DIGEST_LENGTH) { nid_type = NID_sha256; - ret = _libssh2_sha256(m, m_len, hash); + ret = ssh2_ossl_sha256(m, m_len, hash); } else if(hash_len == SHA512_DIGEST_LENGTH) { nid_type = NID_sha512; - ret = _libssh2_sha512(m, m_len, hash); + ret = ssh2_ossl_sha512(m, m_len, hash); } else { /* silence: @@ -488,24 +486,24 @@ int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, } #if LIBSSH2_RSA_SHA1 -int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx *rsactx, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, size_t m_len) +int ssh2_rsa_sha1_verify(ssh2_rsa_ctx *rsactx, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, size_t m_len) { - return _libssh2_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, sig, sig_len, m, - m_len); + return ssh2_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, sig, sig_len, m, + m_len); } #endif #endif #if LIBSSH2_DSA -int _libssh2_dsa_new(libssh2_dsa_ctx **dsactx, - const unsigned char *p, unsigned long p_len, - const unsigned char *q, unsigned long q_len, - const unsigned char *g, unsigned long g_len, - const unsigned char *y, unsigned long y_len, - const unsigned char *x, unsigned long x_len) +int ssh2_dsa_new(ssh2_dsa_ctx **dsactx, + const unsigned char *p, unsigned long p_len, + const unsigned char *q, unsigned long q_len, + const unsigned char *g, unsigned long g_len, + const unsigned char *y, unsigned long y_len, + const unsigned char *x, unsigned long x_len) { #ifdef USE_OPENSSL_3 int ret = 0; @@ -642,9 +640,9 @@ int _libssh2_dsa_new(libssh2_dsa_ctx **dsactx, #endif /* USE_OPENSSL_3 */ } -int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, - const unsigned char *sig, - const unsigned char *m, size_t m_len) +int ssh2_dsa_sha1_verify(ssh2_dsa_ctx *dsactx, + const unsigned char *sig, + const unsigned char *m, size_t m_len) { #ifdef USE_OPENSSL_3 EVP_PKEY_CTX *ctx = NULL; @@ -675,8 +673,8 @@ int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, ctx = EVP_PKEY_CTX_new(dsactx, NULL); der_len = i2d_DSA_SIG(dsasig, &der); - if(ctx && !_libssh2_sha1(m, m_len, hash)) { - /* _libssh2_sha1() succeeded */ + if(ctx && !ssh2_ossl_sha1(m, m_len, hash)) { + /* ssh2_ossl_sha1() succeeded */ if(EVP_PKEY_verify_init(ctx) > 0) { ret = EVP_PKEY_verify(ctx, der, der_len, hash, SHA_DIGEST_LENGTH); } @@ -690,8 +688,8 @@ int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, OPENSSL_clear_free(der, der_len); } #else - if(!_libssh2_sha1(m, m_len, hash)) - /* _libssh2_sha1() succeeded */ + if(!ssh2_ossl_sha1(m, m_len, hash)) + /* ssh2_ossl_sha1() succeeded */ ret = DSA_do_verify(hash, SHA_DIGEST_LENGTH, dsasig, dsactx); #endif @@ -704,25 +702,25 @@ int _libssh2_dsa_sha1_verify(libssh2_dsa_ctx *dsactx, #if LIBSSH2_ECDSA /* - * returns key curve type that maps to libssh2_curve_type + * returns key curve type that maps to ssh2_curve_type */ -libssh2_curve_type _libssh2_ecdsa_get_curve_type(libssh2_ecdsa_ctx *ec_ctx) +ssh2_curve_type ssh2_ecdsa_get_curve_type(ssh2_ecdsa_ctx *ec_ctx) { #ifdef USE_OPENSSL_3 int bits = 0; EVP_PKEY_get_int_param(ec_ctx, OSSL_PKEY_PARAM_BITS, &bits); if(bits == 256) { - return LIBSSH2_EC_CURVE_NISTP256; + return SSH2_EC_CURVE_NISTP256; } else if(bits == 384) { - return LIBSSH2_EC_CURVE_NISTP384; + return SSH2_EC_CURVE_NISTP384; } else if(bits == 521) { - return LIBSSH2_EC_CURVE_NISTP521; + return SSH2_EC_CURVE_NISTP521; } - return LIBSSH2_EC_CURVE_NISTP256; + return SSH2_EC_CURVE_NISTP256; #else const EC_GROUP *group = EC_KEY_get0_group(ec_ctx); return EC_GROUP_get_curve_name(group); @@ -730,22 +728,22 @@ libssh2_curve_type _libssh2_ecdsa_get_curve_type(libssh2_ecdsa_ctx *ec_ctx) } /* - * returns 0 for success, key curve type that maps to libssh2_curve_type + * returns 0 for success, key curve type that maps to ssh2_curve_type */ -int _libssh2_ecdsa_curve_type_from_name(const char *name, - libssh2_curve_type *out_type) +int ssh2_ecdsa_curve_type_from_name(const char *name, + ssh2_curve_type *out_type) { - libssh2_curve_type type; + ssh2_curve_type type; if(!name || strlen(name) != 19) return -1; if(strcmp(name, "ecdsa-sha2-nistp256") == 0) - type = LIBSSH2_EC_CURVE_NISTP256; + type = SSH2_EC_CURVE_NISTP256; else if(strcmp(name, "ecdsa-sha2-nistp384") == 0) - type = LIBSSH2_EC_CURVE_NISTP384; + type = SSH2_EC_CURVE_NISTP384; else if(strcmp(name, "ecdsa-sha2-nistp521") == 0) - type = LIBSSH2_EC_CURVE_NISTP521; + type = SSH2_EC_CURVE_NISTP521; else { return -1; } @@ -760,10 +758,10 @@ int _libssh2_ecdsa_curve_type_from_name(const char *name, /* * Creates a new public key given an octal string, length and type */ -int _libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx **ec_ctx, - const unsigned char *k, - size_t k_len, - libssh2_curve_type curve) +int ssh2_ecdsa_curve_name_with_octal_new(ssh2_ecdsa_ctx **ec_ctx, + const unsigned char *k, + size_t k_len, + ssh2_curve_type curve) { int ret = 0; @@ -849,10 +847,10 @@ int _libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx **ec_ctx, } #ifdef USE_OPENSSL_3 -#define LIBSSH2_ECDSA_VERIFY(digest_type) \ +#define SSH2_OSSL_ECDSA_VERIFY(digest_type) \ do { \ unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \ - if(libssh2_sha##digest_type(m, m_len, hash) == 0) { \ + if(ssh2_sha##digest_type(m, m_len, hash) == 0) { \ ret = EVP_PKEY_verify_init(ctx); \ if(ret > 0) { \ ret = EVP_PKEY_verify(ctx, der, der_len, hash, \ @@ -861,24 +859,23 @@ int _libssh2_ecdsa_curve_name_with_octal_new(libssh2_ecdsa_ctx **ec_ctx, } \ } while(0) #else -#define LIBSSH2_ECDSA_VERIFY(digest_type) \ - do { \ - unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \ - if(libssh2_sha##digest_type(m, m_len, hash) == 0) { \ - ret = ECDSA_do_verify(hash, \ - SHA##digest_type##_DIGEST_LENGTH, \ - ecdsa_sig, ec_key); \ - } \ +#define SSH2_OSSL_ECDSA_VERIFY(digest_type) \ + do { \ + unsigned char hash[SHA##digest_type##_DIGEST_LENGTH]; \ + if(ssh2_sha##digest_type(m, m_len, hash) == 0) { \ + ret = ECDSA_do_verify(hash, SHA##digest_type##_DIGEST_LENGTH, \ + ecdsa_sig, ec_key); \ + } \ } while(0) #endif -int _libssh2_ecdsa_verify(libssh2_ecdsa_ctx *ec_ctx, - const unsigned char *r, size_t r_len, - const unsigned char *s, size_t s_len, - const unsigned char *m, size_t m_len) +int ssh2_ecdsa_verify(ssh2_ecdsa_ctx *ec_ctx, + const unsigned char *r, size_t r_len, + const unsigned char *s, size_t s_len, + const unsigned char *m, size_t m_len) { int ret = 0; - libssh2_curve_type type = _libssh2_ecdsa_get_curve_type(ec_ctx); + ssh2_curve_type type = ssh2_ecdsa_get_curve_type(ec_ctx); #ifdef USE_OPENSSL_3 EVP_PKEY_CTX *ctx = NULL; @@ -919,14 +916,14 @@ int _libssh2_ecdsa_verify(libssh2_ecdsa_ctx *ec_ctx, } #endif - if(type == LIBSSH2_EC_CURVE_NISTP256) { - LIBSSH2_ECDSA_VERIFY(256); + if(type == SSH2_EC_CURVE_NISTP256) { + SSH2_OSSL_ECDSA_VERIFY(256); } - else if(type == LIBSSH2_EC_CURVE_NISTP384) { - LIBSSH2_ECDSA_VERIFY(384); + else if(type == SSH2_EC_CURVE_NISTP384) { + SSH2_OSSL_ECDSA_VERIFY(384); } - else if(type == LIBSSH2_EC_CURVE_NISTP521) { - LIBSSH2_ECDSA_VERIFY(512); + else if(type == SSH2_EC_CURVE_NISTP521) { + SSH2_OSSL_ECDSA_VERIFY(512); } #ifdef USE_OPENSSL_3 @@ -954,9 +951,8 @@ cleanup: #endif /* LIBSSH2_ECDSA */ -int _libssh2_cipher_init(libssh2_cipher_ctx *h, - LIBSSH2_CIPHER_T(algo), - unsigned char *iv, unsigned char *secret, int encrypt) +int ssh2_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo), + unsigned char *iv, unsigned char *secret, int encrypt) { #ifdef HAVE_OPAQUE_STRUCTS #if LIBSSH2_AES_GCM @@ -988,10 +984,9 @@ int _libssh2_cipher_init(libssh2_cipher_ctx *h, #define EVP_MAX_BLOCK_LENGTH 32 #endif -int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, - LIBSSH2_CIPHER_T(algo), - int encrypt, unsigned char *block, size_t blocksize, - int firstlast) +int ssh2_cipher_crypt(ssh2_cipher_ctx *ctx, SSH2_CIPHER_T(algo), + int encrypt, unsigned char *block, size_t blocksize, + int firstlast) { unsigned char buf[EVP_MAX_BLOCK_LENGTH]; int ret = 1; @@ -1104,14 +1099,14 @@ int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, return rc; } -void _libssh2_openssl_crypto_init(void) +void ssh2_ossl_init(void) { #if defined(LIBSSH2_WOLFSSL) && defined(DEBUG_WOLFSSL) wolfSSL_Debugging_ON(); #endif } -void _libssh2_openssl_crypto_exit(void) {} +void ssh2_ossl_exit(void) {} #if LIBSSH2_RSA || LIBSSH2_DSA || LIBSSH2_ECDSA || LIBSSH2_ED25519 /* TODO: Optionally call a passphrase callback specified by the @@ -1155,7 +1150,7 @@ static int read_private_key_from_memory(void **key_ctx, } *key_ctx = read_private_key(bp, NULL, (pem_password_cb *)passphrase_cb, - (void *)LIBSSH2_UNCONST(passphrase)); + SSH2_UNCONST(passphrase)); BIO_free(bp); return (*key_ctx) ? 0 : -1; @@ -1178,7 +1173,7 @@ static int read_private_key_from_file(void **key_ctx, } *key_ctx = read_private_key(bp, NULL, (pem_password_cb *)passphrase_cb, - (void *)LIBSSH2_UNCONST(passphrase)); + SSH2_UNCONST(passphrase)); BIO_free(bp); return (*key_ctx) ? 0 : -1; @@ -1186,11 +1181,11 @@ static int read_private_key_from_file(void **key_ctx, #endif #if LIBSSH2_RSA -int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { int rc; @@ -1202,7 +1197,7 @@ int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, (pem_read_bio_func)&PEM_read_bio_RSAPrivateKey; #endif - _libssh2_init_if_needed(); + ssh2_init_if_needed(); rc = read_private_key_from_memory((void **)rsa, read_rsa, filedata, filedata_len, @@ -1220,7 +1215,7 @@ int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, } static unsigned char *gen_publickey_from_rsa(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsa, + ssh2_rsa_ctx *rsa, size_t *key_len) { int e_bytes, n_bytes; @@ -1257,7 +1252,7 @@ static unsigned char *gen_publickey_from_rsa(LIBSSH2_SESSION *session, /* Key form is "ssh-rsa" + e + n. */ len = 4 + 7 + 4 + e_bytes + 4 + n_bytes; - key = LIBSSH2_ALLOC(session, len); + key = SSH2_ALLOC(session, len); if(!key) { goto fail; } @@ -1265,7 +1260,7 @@ static unsigned char *gen_publickey_from_rsa(LIBSSH2_SESSION *session, /* Process key encoding. */ p = key; - _libssh2_htonu32(p, 7); /* Key type. */ + ssh2_htonu32(p, 7); /* Key type. */ p += 4; /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */ memcpy(p, "ssh-rsa", 7); @@ -1290,13 +1285,13 @@ static int gen_publickey_from_rsa_evp(LIBSSH2_SESSION *session, size_t *pubkeydata_len, EVP_PKEY *pk) { - libssh2_rsa_ctx *rsa = NULL; + ssh2_rsa_ctx *rsa = NULL; unsigned char *key; unsigned char *method_buf = NULL; size_t key_len; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from RSA private key envelope")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from RSA private key envelope")); #ifdef USE_OPENSSL_3 rsa = pk; @@ -1308,7 +1303,7 @@ static int gen_publickey_from_rsa_evp(LIBSSH2_SESSION *session, goto alloc_error; } - method_buf = LIBSSH2_ALLOC(session, 7); /* ssh-rsa. */ + method_buf = SSH2_ALLOC(session, 7); /* ssh-rsa. */ if(!method_buf) { goto alloc_error; } @@ -1340,15 +1335,15 @@ alloc_error: } #endif if(method_buf) { - LIBSSH2_FREE(session, method_buf); + SSH2_FREE(session, method_buf); } - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for private key data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for private key data"); } #ifndef USE_OPENSSL_3 -static int rsa_new_additional_parameters(libssh2_rsa_ctx *rsa) +static int rsa_new_additional_parameters(ssh2_rsa_ctx *rsa) { BN_CTX *ctx = NULL; BIGNUM *aux = NULL; @@ -1428,64 +1423,64 @@ static int gen_publickey_from_rsa_openssh_priv_data( size_t *method_len, unsigned char **pubkeydata, size_t *pubkeydata_len, - libssh2_rsa_ctx **rsa_ctx) + ssh2_rsa_ctx **rsa_ctx) { int rc = 0; size_t nlen, elen, dlen, plen, qlen, coefflen, commentlen; unsigned char *n, *e, *d, *p, *q, *coeff, *comment; - libssh2_rsa_ctx *rsa = NULL; + ssh2_rsa_ctx *rsa = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing RSA keys from private key data")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing RSA keys from private key data")); /* public key data */ - if(_libssh2_get_bignum_bytes(decrypted, &n, &nlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "RSA no n"); + if(ssh2_get_bignum_bytes(decrypted, &n, &nlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "RSA no n"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &e, &elen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "RSA no e"); + if(ssh2_get_bignum_bytes(decrypted, &e, &elen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "RSA no e"); return -1; } /* private key data */ - if(_libssh2_get_bignum_bytes(decrypted, &d, &dlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "RSA no d"); + if(ssh2_get_bignum_bytes(decrypted, &d, &dlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "RSA no d"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &coeff, &coefflen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "RSA no coeff"); + if(ssh2_get_bignum_bytes(decrypted, &coeff, &coefflen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "RSA no coeff"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &p, &plen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "RSA no p"); + if(ssh2_get_bignum_bytes(decrypted, &p, &plen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "RSA no p"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &q, &qlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "RSA no q"); + if(ssh2_get_bignum_bytes(decrypted, &q, &qlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "RSA no q"); return -1; } - if(_libssh2_get_string(decrypted, &comment, &commentlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "RSA no comment"); + if(ssh2_get_string(decrypted, &comment, &commentlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "RSA no comment"); return -1; } - rc = _libssh2_rsa_new(&rsa, - e, (unsigned long)elen, - n, (unsigned long)nlen, - d, (unsigned long)dlen, - p, (unsigned long)plen, - q, (unsigned long)qlen, - NULL, 0, NULL, 0, - coeff, (unsigned long)coefflen); + rc = ssh2_rsa_new(&rsa, + e, (unsigned long)elen, + n, (unsigned long)nlen, + d, (unsigned long)dlen, + p, (unsigned long)plen, + q, (unsigned long)qlen, + NULL, 0, NULL, 0, + coeff, (unsigned long)coefflen); if(rc) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Could not create RSA private key")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Could not create RSA private key")); goto fail; } @@ -1514,20 +1509,20 @@ static int gen_publickey_from_rsa_openssh_priv_data( if(rsa_ctx) *rsa_ctx = rsa; else - _libssh2_rsa_free(rsa); + ssh2_rsa_free(rsa); return rc; fail: if(rsa) - _libssh2_rsa_free(rsa); + ssh2_rsa_free(rsa); - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for private key data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for private key data"); } -static int rsa_new_openssh_private(libssh2_rsa_ctx **rsa, +static int rsa_new_openssh_private(ssh2_rsa_ctx **rsa, LIBSSH2_SESSION *session, const char *filename, const unsigned char *passphrase) @@ -1538,31 +1533,31 @@ static int rsa_new_openssh_private(libssh2_rsa_ctx **rsa, struct string_buf *decrypted = NULL; if(!session) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Session is required"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); return -1; } - _libssh2_init_if_needed(); + ssh2_init_if_needed(); fp = fopen(filename, "r"); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open OpenSSH RSA private key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open OpenSSH RSA private key file"); return -1; } - rc = _libssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); + rc = ssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); fclose(fp); if(rc) { return rc; } /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted key data not found"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); return -1; } @@ -1576,15 +1571,15 @@ static int rsa_new_openssh_private(libssh2_rsa_ctx **rsa, } if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); return rc; } -int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_rsa_new_private(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { int rc; @@ -1596,7 +1591,7 @@ int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, (pem_read_bio_func)&PEM_read_bio_RSAPrivateKey; #endif - _libssh2_init_if_needed(); + ssh2_init_if_needed(); rc = read_private_key_from_file((void **)rsa, read_rsa, filename, passphrase); @@ -1611,11 +1606,11 @@ int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, #endif #if LIBSSH2_DSA -int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_dsa_new_private_frommemory(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { int rc; @@ -1627,7 +1622,7 @@ int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx **dsa, (pem_read_bio_func)&PEM_read_bio_DSAPrivateKey; #endif - _libssh2_init_if_needed(); + ssh2_init_if_needed(); rc = read_private_key_from_memory((void **)dsa, read_dsa, filedata, filedata_len, @@ -1645,7 +1640,7 @@ int _libssh2_dsa_new_private_frommemory(libssh2_dsa_ctx **dsa, } static unsigned char *gen_publickey_from_dsa(LIBSSH2_SESSION *session, - libssh2_dsa_ctx *dsa, + ssh2_dsa_ctx *dsa, size_t *key_len) { int p_bytes, q_bytes, g_bytes, k_bytes; @@ -1690,7 +1685,7 @@ static unsigned char *gen_publickey_from_dsa(LIBSSH2_SESSION *session, /* Key form is "ssh-dss" + p + q + g + pub_key. */ len = 4 + 7 + 4 + p_bytes + 4 + q_bytes + 4 + g_bytes + 4 + k_bytes; - key = LIBSSH2_ALLOC(session, len); + key = SSH2_ALLOC(session, len); if(!key) { goto fail; } @@ -1698,7 +1693,7 @@ static unsigned char *gen_publickey_from_dsa(LIBSSH2_SESSION *session, /* Process key encoding. */ p = key; - _libssh2_htonu32(p, 7); /* Key type. */ + ssh2_htonu32(p, 7); /* Key type. */ p += 4; memcpy(p, "ssh-dss", 7); p += 7; @@ -1726,13 +1721,13 @@ static int gen_publickey_from_dsa_evp(LIBSSH2_SESSION *session, size_t *pubkeydata_len, EVP_PKEY *pk) { - libssh2_dsa_ctx *dsa = NULL; + ssh2_dsa_ctx *dsa = NULL; unsigned char *key; unsigned char *method_buf = NULL; size_t key_len; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from DSA private key envelope")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from DSA private key envelope")); #ifdef USE_OPENSSL_3 dsa = pk; @@ -1744,7 +1739,7 @@ static int gen_publickey_from_dsa_evp(LIBSSH2_SESSION *session, goto alloc_error; } - method_buf = LIBSSH2_ALLOC(session, 7); /* ssh-dss. */ + method_buf = SSH2_ALLOC(session, 7); /* ssh-dss. */ if(!method_buf) { goto alloc_error; } @@ -1775,11 +1770,11 @@ alloc_error: } #endif if(method_buf) { - LIBSSH2_FREE(session, method_buf); + SSH2_FREE(session, method_buf); } - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for private key data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for private key data"); } static int gen_publickey_from_dsa_openssh_priv_data( @@ -1789,50 +1784,50 @@ static int gen_publickey_from_dsa_openssh_priv_data( size_t *method_len, unsigned char **pubkeydata, size_t *pubkeydata_len, - libssh2_dsa_ctx **dsa_ctx) + ssh2_dsa_ctx **dsa_ctx) { int rc = 0; size_t plen, qlen, glen, pub_len, priv_len; unsigned char *p, *q, *g, *pub_key, *priv_key; - libssh2_dsa_ctx *dsa = NULL; + ssh2_dsa_ctx *dsa = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing DSA keys from private key data")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing DSA keys from private key data")); - if(_libssh2_get_bignum_bytes(decrypted, &p, &plen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "DSA no p"); + if(ssh2_get_bignum_bytes(decrypted, &p, &plen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "DSA no p"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &q, &qlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "DSA no q"); + if(ssh2_get_bignum_bytes(decrypted, &q, &qlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "DSA no q"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &g, &glen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "DSA no g"); + if(ssh2_get_bignum_bytes(decrypted, &g, &glen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "DSA no g"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &pub_key, &pub_len)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "DSA no public key"); + if(ssh2_get_bignum_bytes(decrypted, &pub_key, &pub_len)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "DSA no public key"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &priv_key, &priv_len)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "DSA no private key"); + if(ssh2_get_bignum_bytes(decrypted, &priv_key, &priv_len)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "DSA no private key"); return -1; } - rc = _libssh2_dsa_new(&dsa, - p, (unsigned long)plen, - q, (unsigned long)qlen, - g, (unsigned long)glen, - pub_key, (unsigned long)pub_len, - priv_key, (unsigned long)priv_len); + rc = ssh2_dsa_new(&dsa, + p, (unsigned long)plen, + q, (unsigned long)qlen, + g, (unsigned long)glen, + pub_key, (unsigned long)pub_len, + priv_key, (unsigned long)priv_len); if(rc) { - _libssh2_debug((session, LIBSSH2_ERROR_PROTO, - "Could not create DSA private key")); + ssh2_deb((session, LIBSSH2_ERROR_PROTO, + "Could not create DSA private key")); goto fail; } @@ -1856,20 +1851,20 @@ static int gen_publickey_from_dsa_openssh_priv_data( if(dsa_ctx) *dsa_ctx = dsa; else - _libssh2_dsa_free(dsa); + ssh2_dsa_free(dsa); return rc; fail: if(dsa) - _libssh2_dsa_free(dsa); + ssh2_dsa_free(dsa); - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for private key data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for private key data"); } -static int dsa_new_openssh_private(libssh2_dsa_ctx **dsa, +static int dsa_new_openssh_private(ssh2_dsa_ctx **dsa, LIBSSH2_SESSION *session, const char *filename, const unsigned char *passphrase) @@ -1880,31 +1875,31 @@ static int dsa_new_openssh_private(libssh2_dsa_ctx **dsa, struct string_buf *decrypted = NULL; if(!session) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Session is required"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); return -1; } - _libssh2_init_if_needed(); + ssh2_init_if_needed(); fp = fopen(filename, "r"); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open OpenSSH DSA private key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open OpenSSH DSA private key file"); return -1; } - rc = _libssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); + rc = ssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); fclose(fp); if(rc) { return rc; } /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted key data not found"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); return -1; } @@ -1918,15 +1913,15 @@ static int dsa_new_openssh_private(libssh2_dsa_ctx **dsa, } if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); return rc; } -int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_dsa_new_private(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { int rc; @@ -1938,7 +1933,7 @@ int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, (pem_read_bio_func)&PEM_read_bio_DSAPrivateKey; #endif - _libssh2_init_if_needed(); + ssh2_init_if_needed(); rc = read_private_key_from_file((void **)dsa, read_dsa, filename, passphrase); @@ -1952,11 +1947,11 @@ int _libssh2_dsa_new_private(libssh2_dsa_ctx **dsa, #endif /* LIBSSH_DSA */ #if LIBSSH2_ECDSA -int _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_ecdsa_new_private_frommemory(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { int rc; @@ -1968,7 +1963,7 @@ int _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx **ec_ctx, (pem_read_bio_func)&PEM_read_bio_ECPrivateKey; #endif - _libssh2_init_if_needed(); + ssh2_init_if_needed(); rc = read_private_key_from_memory((void **)ec_ctx, read_ec, filedata, filedata_len, @@ -1985,15 +1980,15 @@ int _libssh2_ecdsa_new_private_frommemory(libssh2_ecdsa_ctx **ec_ctx, return rc; } -int _libssh2_ecdsa_new_private_frommemory_sk(libssh2_ecdsa_ctx **ec_ctx, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_ecdsa_new_private_frommemory_sk(ssh2_ecdsa_ctx **ec_ctx, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { int algorithm; return sk_pub_openssh_keyfilememory(session, @@ -2017,9 +2012,9 @@ int _libssh2_ecdsa_new_private_frommemory_sk(libssh2_ecdsa_ctx **ec_ctx, #if LIBSSH2_ED25519 -int _libssh2_curve25519_new(LIBSSH2_SESSION *session, - unsigned char **out_public_key, - unsigned char **out_private_key) +int ssh2_curve25519_new(LIBSSH2_SESSION *session, + unsigned char **out_public_key, + unsigned char **out_private_key) { EVP_PKEY *key = NULL; EVP_PKEY_CTX *pctx = NULL; @@ -2037,13 +2032,13 @@ int _libssh2_curve25519_new(LIBSSH2_SESSION *session, } if(out_private_key) { - privLen = LIBSSH2_ED25519_KEY_LEN; - priv = LIBSSH2_ALLOC(session, privLen); + privLen = SSH2_ED25519_KEY_LEN; + priv = SSH2_ALLOC(session, privLen); if(!priv) goto clean_exit; if(EVP_PKEY_get_raw_private_key(key, priv, &privLen) != 1 || - privLen != LIBSSH2_ED25519_KEY_LEN) { + privLen != SSH2_ED25519_KEY_LEN) { goto clean_exit; } @@ -2052,13 +2047,13 @@ int _libssh2_curve25519_new(LIBSSH2_SESSION *session, } if(out_public_key) { - pubLen = LIBSSH2_ED25519_KEY_LEN; - pub = LIBSSH2_ALLOC(session, pubLen); + pubLen = SSH2_ED25519_KEY_LEN; + pub = SSH2_ALLOC(session, pubLen); if(!pub) goto clean_exit; if(EVP_PKEY_get_raw_public_key(key, pub, &pubLen) != 1 || - pubLen != LIBSSH2_ED25519_KEY_LEN) { + pubLen != SSH2_ED25519_KEY_LEN) { goto clean_exit; } @@ -2076,9 +2071,9 @@ clean_exit: if(key) EVP_PKEY_free(key); if(priv) - LIBSSH2_FREE(session, priv); + SSH2_FREE(session, priv); if(pub) - LIBSSH2_FREE(session, pub); + SSH2_FREE(session, pub); return rc; } @@ -2097,38 +2092,38 @@ static int gen_publickey_from_ed_evp(LIBSSH2_SESSION *session, size_t bufLen = 0; unsigned char *bufPos = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from ED private key envelope")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from ED private key envelope")); - methodBuf = LIBSSH2_ALLOC(session, sizeof(methodName) - 1); + methodBuf = SSH2_ALLOC(session, sizeof(methodName) - 1); if(!methodBuf) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for private key data"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for private key data"); goto fail; } memcpy(methodBuf, methodName, sizeof(methodName) - 1); if(EVP_PKEY_get_raw_public_key(pk, NULL, &rawKeyLen) != 1) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "EVP_PKEY_get_raw_public_key failed"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "EVP_PKEY_get_raw_public_key failed"); goto fail; } /* Key form is: type_len(4) + type(11) + pub_key_len(4) + pub_key(32). */ bufLen = 4 + sizeof(methodName) - 1 + 4 + rawKeyLen; - bufPos = keyBuf = LIBSSH2_ALLOC(session, bufLen); + bufPos = keyBuf = SSH2_ALLOC(session, bufLen); if(!keyBuf) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for private key data"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for private key data"); goto fail; } - _libssh2_store_str(&bufPos, methodName, sizeof(methodName) - 1); - _libssh2_store_u32(&bufPos, (uint32_t)rawKeyLen); + ssh2_store_str(&bufPos, methodName, sizeof(methodName) - 1); + ssh2_store_u32(&bufPos, (uint32_t)rawKeyLen); if(EVP_PKEY_get_raw_public_key(pk, bufPos, &rawKeyLen) != 1) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "EVP_PKEY_get_raw_public_key failed"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "EVP_PKEY_get_raw_public_key failed"); goto fail; } @@ -2144,9 +2139,9 @@ static int gen_publickey_from_ed_evp(LIBSSH2_SESSION *session, fail: if(methodBuf) - LIBSSH2_FREE(session, methodBuf); + SSH2_FREE(session, methodBuf); if(keyBuf) - LIBSSH2_FREE(session, keyBuf); + SSH2_FREE(session, keyBuf); return -1; } @@ -2157,9 +2152,9 @@ static int gen_publickey_from_ed25519_openssh_priv_data( size_t *method_len, unsigned char **pubkeydata, size_t *pubkeydata_len, - libssh2_ed25519_ctx **out_ctx) + ssh2_ed25519_ctx **out_ctx) { - libssh2_ed25519_ctx *ctx = NULL; + ssh2_ed25519_ctx *ctx = NULL; unsigned char *method_buf = NULL; unsigned char *key = NULL; int i, ret = 0; @@ -2167,20 +2162,18 @@ static int gen_publickey_from_ed25519_openssh_priv_data( size_t key_len = 0, tmp_len = 0; unsigned char *p; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing ED25519 keys from private key data")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing ED25519 keys from private key data")); - if(_libssh2_get_string(decrypted, &pub_key, &tmp_len) || - tmp_len != LIBSSH2_ED25519_KEY_LEN) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Wrong public key length"); + if(ssh2_get_string(decrypted, &pub_key, &tmp_len) || + tmp_len != SSH2_ED25519_KEY_LEN) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Wrong public key length"); return -1; } - if(_libssh2_get_string(decrypted, &priv_key, &tmp_len) || - tmp_len != LIBSSH2_ED25519_PRIVATE_KEY_LEN) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Wrong private key length"); + if(ssh2_get_string(decrypted, &priv_key, &tmp_len) || + tmp_len != SSH2_ED25519_PRIVATE_KEY_LEN) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Wrong private key length"); goto clean_exit; } @@ -2188,25 +2181,25 @@ static int gen_publickey_from_ed25519_openssh_priv_data( the public key */ ctx = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, NULL, (const unsigned char *)priv_key, - LIBSSH2_ED25519_KEY_LEN); + SSH2_ED25519_KEY_LEN); /* comment */ - if(_libssh2_get_string(decrypted, &buf, &tmp_len)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Unable to read comment"); + if(ssh2_get_string(decrypted, &buf, &tmp_len)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Unable to read comment"); goto clean_exit; } if(tmp_len > 0) { - unsigned char *comment = LIBSSH2_CALLOC(session, tmp_len + 1); + unsigned char *comment = SSH2_CALLOC(session, tmp_len + 1); if(comment) { memcpy(comment, buf, tmp_len); /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */ memcpy(comment + tmp_len, "\0", 1); - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, "Key comment: %s", - comment)); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, "Key comment: %s", + comment)); - LIBSSH2_FREE(session, comment); + SSH2_FREE(session, comment); } } @@ -2214,7 +2207,7 @@ static int gen_publickey_from_ed25519_openssh_priv_data( i = 1; while(decrypted->dataptr < decrypted->data + decrypted->len) { if(*decrypted->dataptr != i) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Wrong padding"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Wrong padding"); goto clean_exit; } i++; @@ -2222,31 +2215,31 @@ static int gen_publickey_from_ed25519_openssh_priv_data( } if(ret == 0) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from ED25519 " - "private key envelope")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from ED25519 " + "private key envelope")); - method_buf = LIBSSH2_ALLOC(session, 11); /* ssh-ed25519. */ + method_buf = SSH2_ALLOC(session, 11); /* ssh-ed25519. */ if(!method_buf) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for ED25519 key"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for ED25519 key"); goto clean_exit; } /* Key form is: type_len(4) + type(11) + pub_key_len(4) + pub_key(32). */ - key_len = LIBSSH2_ED25519_KEY_LEN + 19; - key = LIBSSH2_CALLOC(session, key_len); + key_len = SSH2_ED25519_KEY_LEN + 19; + key = SSH2_CALLOC(session, key_len); if(!key) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for ED25519 key"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for ED25519 key"); goto clean_exit; } p = key; - _libssh2_store_str(&p, "ssh-ed25519", 11); - _libssh2_store_str(&p, (const char *)pub_key, LIBSSH2_ED25519_KEY_LEN); + ssh2_store_str(&p, "ssh-ed25519", 11); + ssh2_store_str(&p, (const char *)pub_key, SSH2_ED25519_KEY_LEN); /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */ memcpy(method_buf, "ssh-ed25519", 11); @@ -2254,7 +2247,7 @@ static int gen_publickey_from_ed25519_openssh_priv_data( if(method) *method = method_buf; else - LIBSSH2_FREE(session, method_buf); + SSH2_FREE(session, method_buf); if(method_len) *method_len = 11; @@ -2262,7 +2255,7 @@ static int gen_publickey_from_ed25519_openssh_priv_data( if(pubkeydata) *pubkeydata = key; else - LIBSSH2_FREE(session, key); + SSH2_FREE(session, key); if(pubkeydata_len) *pubkeydata_len = key_len; @@ -2270,7 +2263,7 @@ static int gen_publickey_from_ed25519_openssh_priv_data( if(out_ctx) *out_ctx = ctx; else if(ctx) - _libssh2_ed25519_free(ctx); + ssh2_ed25519_free(ctx); return 0; } @@ -2278,13 +2271,13 @@ static int gen_publickey_from_ed25519_openssh_priv_data( clean_exit: if(ctx) - _libssh2_ed25519_free(ctx); + ssh2_ed25519_free(ctx); if(method_buf) - LIBSSH2_FREE(session, method_buf); + SSH2_FREE(session, method_buf); if(key) - LIBSSH2_FREE(session, key); + SSH2_FREE(session, key); return -1; } @@ -2300,11 +2293,11 @@ static int gen_publickey_from_sk_ed25519_openssh_priv_data( const char **application, const unsigned char **key_handle, size_t *handle_len, - libssh2_ed25519_ctx **out_ctx) + ssh2_ed25519_ctx **out_ctx) { const char *key_type = "sk-ssh-ed25519@openssh.com"; - libssh2_ed25519_ctx *ctx = NULL; + ssh2_ed25519_ctx *ctx = NULL; unsigned char *method_buf = NULL; unsigned char *key = NULL; int ret = 0; @@ -2312,81 +2305,78 @@ static int gen_publickey_from_sk_ed25519_openssh_priv_data( size_t key_len = 0, app_len = 0, tmp_len = 0; unsigned char *p; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing sk-ED25519 keys from private key data")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing sk-ED25519 keys from private key data")); - if(_libssh2_get_string(decrypted, &pub_key, &tmp_len) || - tmp_len != LIBSSH2_ED25519_KEY_LEN) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Wrong public key length"); + if(ssh2_get_string(decrypted, &pub_key, &tmp_len) || + tmp_len != SSH2_ED25519_KEY_LEN) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Wrong public key length"); return -1; } - if(_libssh2_get_string(decrypted, &app, &app_len)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "No SK application."); + if(ssh2_get_string(decrypted, &app, &app_len)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "No SK application."); return -1; } - if(flags && _libssh2_get_byte(decrypted, flags)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "No SK flags."); + if(flags && ssh2_get_byte(decrypted, flags)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "No SK flags."); return -1; } if(key_handle && handle_len) { unsigned char *handle = NULL; - if(_libssh2_get_string(decrypted, &handle, handle_len)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "No SK key_handle."); + if(ssh2_get_string(decrypted, &handle, handle_len)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "No SK key_handle."); return -1; } if(*handle_len > 0) { - *key_handle = LIBSSH2_ALLOC(session, *handle_len); + *key_handle = SSH2_ALLOC(session, *handle_len); if(key_handle && *key_handle) { - memcpy((void *)LIBSSH2_UNCONST(*key_handle), - handle, *handle_len); + memcpy(SSH2_UNCONST(*key_handle), handle, *handle_len); } } } ctx = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, (const unsigned char *)pub_key, - LIBSSH2_ED25519_KEY_LEN); + SSH2_ED25519_KEY_LEN); if(ret == 0) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from ED25519 " - "private key envelope")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from ED25519 " + "private key envelope")); /* sk-ssh-ed25519@openssh.com. */ - method_buf = LIBSSH2_ALLOC(session, strlen(key_type)); + method_buf = SSH2_ALLOC(session, strlen(key_type)); if(!method_buf) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for ED25519 key"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for ED25519 key"); goto clean_exit; } /* Key form is: type_len(4) + type(26) + pub_key_len(4) + pub_key(32) + application_len(4) + application(X). */ - key_len = LIBSSH2_ED25519_KEY_LEN + 38 + app_len; - key = LIBSSH2_CALLOC(session, key_len); + key_len = SSH2_ED25519_KEY_LEN + 38 + app_len; + key = SSH2_CALLOC(session, key_len); if(!key) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for ED25519 key"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for ED25519 key"); goto clean_exit; } p = key; - _libssh2_store_str(&p, key_type, strlen(key_type)); - _libssh2_store_str(&p, (const char *)pub_key, LIBSSH2_ED25519_KEY_LEN); - _libssh2_store_str(&p, (const char *)app, app_len); + ssh2_store_str(&p, key_type, strlen(key_type)); + ssh2_store_str(&p, (const char *)pub_key, SSH2_ED25519_KEY_LEN); + ssh2_store_str(&p, (const char *)app, app_len); if(application && app_len > 0) { - *application = LIBSSH2_ALLOC(session, app_len + 1); - _libssh2_explicit_zero((void *)LIBSSH2_UNCONST(*application), - app_len + 1); - memcpy((void *)LIBSSH2_UNCONST(*application), app, app_len); + *application = SSH2_ALLOC(session, app_len + 1); + ssh2_explicit_zero(SSH2_UNCONST(*application), app_len + 1); + memcpy(SSH2_UNCONST(*application), app, app_len); } /* NOLINTNEXTLINE(bugprone-not-null-terminated-result) */ @@ -2395,7 +2385,7 @@ static int gen_publickey_from_sk_ed25519_openssh_priv_data( if(method) *method = method_buf; else - LIBSSH2_FREE(session, method_buf); + SSH2_FREE(session, method_buf); if(method_len) *method_len = strlen(key_type); @@ -2403,7 +2393,7 @@ static int gen_publickey_from_sk_ed25519_openssh_priv_data( if(pubkeydata) *pubkeydata = key; else if(key) - LIBSSH2_FREE(session, key); + SSH2_FREE(session, key); if(pubkeydata_len) *pubkeydata_len = key_len; @@ -2411,7 +2401,7 @@ static int gen_publickey_from_sk_ed25519_openssh_priv_data( if(out_ctx) *out_ctx = ctx; else if(ctx) - _libssh2_ed25519_free(ctx); + ssh2_ed25519_free(ctx); return 0; } @@ -2419,64 +2409,64 @@ static int gen_publickey_from_sk_ed25519_openssh_priv_data( clean_exit: if(ctx) - _libssh2_ed25519_free(ctx); + ssh2_ed25519_free(ctx); if(method_buf) - LIBSSH2_FREE(session, method_buf); + SSH2_FREE(session, method_buf); if(key) - LIBSSH2_FREE(session, key); + SSH2_FREE(session, key); if(application && *application) { - LIBSSH2_FREE(session, (void *)LIBSSH2_UNCONST(*application)); + SSH2_FREE(session, SSH2_UNCONST(*application)); *application = NULL; } if(key_handle && *key_handle) { - LIBSSH2_FREE(session, (void *)LIBSSH2_UNCONST(*key_handle)); + SSH2_FREE(session, SSH2_UNCONST(*key_handle)); *key_handle = NULL; } return -1; } -int _libssh2_ed25519_new_private(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const uint8_t *passphrase) +int ssh2_ed25519_new_private(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const uint8_t *passphrase) { int rc; FILE *fp; unsigned char *buf; struct string_buf *decrypted = NULL; - libssh2_ed25519_ctx *ctx = NULL; + ssh2_ed25519_ctx *ctx = NULL; if(!session) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Session is required"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); return -1; } - _libssh2_init_if_needed(); + ssh2_init_if_needed(); fp = fopen(filename, "r"); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open ED25519 private key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open ED25519 private key file"); return -1; } - rc = _libssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); + rc = ssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); fclose(fp); if(rc) { return rc; } /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted key data not found"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); return -1; } @@ -2491,59 +2481,59 @@ int _libssh2_ed25519_new_private(libssh2_ed25519_ctx **ed_ctx, } if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); if(rc == 0) { if(ed_ctx) *ed_ctx = ctx; else if(ctx) - _libssh2_ed25519_free(ctx); + ssh2_ed25519_free(ctx); } return rc; } -int _libssh2_ed25519_new_private_sk(libssh2_ed25519_ctx **ed_ctx, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - LIBSSH2_SESSION *session, - const char *filename, - const uint8_t *passphrase) +int ssh2_ed25519_new_private_sk(ssh2_ed25519_ctx **ed_ctx, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + LIBSSH2_SESSION *session, + const char *filename, + const uint8_t *passphrase) { int rc; FILE *fp; unsigned char *buf; struct string_buf *decrypted = NULL; - libssh2_ed25519_ctx *ctx = NULL; + ssh2_ed25519_ctx *ctx = NULL; if(!session) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Session is required"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); return -1; } - _libssh2_init_if_needed(); + ssh2_init_if_needed(); fp = fopen(filename, "r"); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open ED25519 SK private key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open ED25519 SK private key file"); return -1; } - rc = _libssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); + rc = ssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); fclose(fp); if(rc) { return rc; } /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted key data not found"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); return -1; } @@ -2563,27 +2553,27 @@ int _libssh2_ed25519_new_private_sk(libssh2_ed25519_ctx **ed_ctx, } if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); if(rc == 0) { if(ed_ctx) *ed_ctx = ctx; else if(ctx) - _libssh2_ed25519_free(ctx); + ssh2_ed25519_free(ctx); } return rc; } -int _libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_ed25519_new_private_frommemory(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { - libssh2_ed25519_ctx *ctx = NULL; + ssh2_ed25519_ctx *ctx = NULL; - _libssh2_init_if_needed(); + ssh2_init_if_needed(); if(read_private_key_from_memory((void **)&ctx, (pem_read_bio_func) @@ -2591,9 +2581,9 @@ int _libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx **ed_ctx, filedata, filedata_len, passphrase) == 0) { if(EVP_PKEY_id(ctx) != EVP_PKEY_ED25519) { - _libssh2_ed25519_free(ctx); - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Private key is not an ED25519 key"); + ssh2_ed25519_free(ctx); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Private key is not an ED25519 key"); } *ed_ctx = ctx; @@ -2607,16 +2597,15 @@ int _libssh2_ed25519_new_private_frommemory(libssh2_ed25519_ctx **ed_ctx, passphrase); } -int _libssh2_ed25519_new_private_frommemory_sk( - libssh2_ed25519_ctx **ed_ctx, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_ed25519_new_private_frommemory_sk(ssh2_ed25519_ctx **ed_ctx, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { int algorithm; return sk_pub_openssh_keyfilememory(session, @@ -2636,12 +2625,12 @@ int _libssh2_ed25519_new_private_frommemory_sk( passphrase); } -int _libssh2_ed25519_new_public(libssh2_ed25519_ctx **ed_ctx, - LIBSSH2_SESSION *session, - const unsigned char *raw_pub_key, - const size_t key_len) +int ssh2_ed25519_new_public(ssh2_ed25519_ctx **ed_ctx, + LIBSSH2_SESSION *session, + const unsigned char *raw_pub_key, + const size_t key_len) { - libssh2_ed25519_ctx *ctx = NULL; + ssh2_ed25519_ctx *ctx = NULL; if(!ed_ctx) return -1; @@ -2649,13 +2638,13 @@ int _libssh2_ed25519_new_public(libssh2_ed25519_ctx **ed_ctx, ctx = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, raw_pub_key, key_len); if(!ctx) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "could not create ED25519 public key"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "could not create ED25519 public key"); if(ed_ctx) *ed_ctx = ctx; else if(ctx) - _libssh2_ed25519_free(ctx); + ssh2_ed25519_free(ctx); return 0; } @@ -2663,10 +2652,10 @@ int _libssh2_ed25519_new_public(libssh2_ed25519_ctx **ed_ctx, #if LIBSSH2_MLKEM -int _libssh2_mlkem_new(LIBSSH2_SESSION *session, - int ml_kem_size, - unsigned char **out_public_key, - unsigned char **out_private_key) +int ssh2_mlkem_new(LIBSSH2_SESSION *session, + int ml_kem_size, + unsigned char **out_public_key, + unsigned char **out_private_key) { EVP_PKEY *key = NULL; EVP_PKEY_CTX *pctx = NULL; @@ -2678,18 +2667,18 @@ int _libssh2_mlkem_new(LIBSSH2_SESSION *session, switch(ml_kem_size) { case 512: evp_name = "ML-KEM-512"; - privLen = LIBSSH2_MLKEM_512_PRIVATE_KEY_LEN; - pubLen = LIBSSH2_MLKEM_512_PUBLIC_KEY_LEN; + privLen = SSH2_MLKEM_512_PRIVATE_KEY_LEN; + pubLen = SSH2_MLKEM_512_PUBLIC_KEY_LEN; break; case 768: evp_name = "ML-KEM-768"; - privLen = LIBSSH2_MLKEM_768_PRIVATE_KEY_LEN; - pubLen = LIBSSH2_MLKEM_768_PUBLIC_KEY_LEN; + privLen = SSH2_MLKEM_768_PRIVATE_KEY_LEN; + pubLen = SSH2_MLKEM_768_PUBLIC_KEY_LEN; break; case 1024: evp_name = "ML-KEM-1024"; - privLen = LIBSSH2_MLKEM_1024_PRIVATE_KEY_LEN; - pubLen = LIBSSH2_MLKEM_1024_PUBLIC_KEY_LEN; + privLen = SSH2_MLKEM_1024_PRIVATE_KEY_LEN; + pubLen = SSH2_MLKEM_1024_PUBLIC_KEY_LEN; break; default: goto clean_exit; @@ -2705,7 +2694,7 @@ int _libssh2_mlkem_new(LIBSSH2_SESSION *session, } if(out_private_key) { - priv = LIBSSH2_ALLOC(session, privLen); + priv = SSH2_ALLOC(session, privLen); actualPrivLen = privLen; if(!priv) goto clean_exit; @@ -2719,7 +2708,7 @@ int _libssh2_mlkem_new(LIBSSH2_SESSION *session, } if(out_public_key) { - pub = LIBSSH2_ALLOC(session, pubLen); + pub = SSH2_ALLOC(session, pubLen); if(!pub) goto clean_exit; @@ -2742,17 +2731,17 @@ clean_exit: if(key) EVP_PKEY_free(key); if(priv) - LIBSSH2_FREE(session, priv); + SSH2_FREE(session, priv); if(pub) - LIBSSH2_FREE(session, pub); + SSH2_FREE(session, pub); return rc; } -int _libssh2_mlkem_get_sk(unsigned char *out_shared_key, - int ml_kem_size, - uint8_t *private_key, - uint8_t *server_ciphertext) +int ssh2_mlkem_get_sk(unsigned char *out_shared_key, + int ml_kem_size, + uint8_t *private_key, + uint8_t *server_ciphertext) { int rc = -1; EVP_PKEY *client_key = NULL; @@ -2764,18 +2753,18 @@ int _libssh2_mlkem_get_sk(unsigned char *out_shared_key, switch(ml_kem_size) { case 512: evp_name = "ML-KEM-512"; - privLen = LIBSSH2_MLKEM_512_PRIVATE_KEY_LEN; - ciphertextLen = LIBSSH2_MLKEM_512_CIPHERTEXT; + privLen = SSH2_MLKEM_512_PRIVATE_KEY_LEN; + ciphertextLen = SSH2_MLKEM_512_CIPHERTEXT; break; case 768: evp_name = "ML-KEM-768"; - privLen = LIBSSH2_MLKEM_768_PRIVATE_KEY_LEN; - ciphertextLen = LIBSSH2_MLKEM_768_CIPHERTEXT; + privLen = SSH2_MLKEM_768_PRIVATE_KEY_LEN; + ciphertextLen = SSH2_MLKEM_768_CIPHERTEXT; break; case 1024: evp_name = "ML-KEM-1024"; - privLen = LIBSSH2_MLKEM_1024_PRIVATE_KEY_LEN; - ciphertextLen = LIBSSH2_MLKEM_1024_CIPHERTEXT; + privLen = SSH2_MLKEM_1024_PRIVATE_KEY_LEN; + ciphertextLen = SSH2_MLKEM_1024_CIPHERTEXT; break; default: goto clean_exit; @@ -2807,7 +2796,7 @@ int _libssh2_mlkem_get_sk(unsigned char *out_shared_key, goto clean_exit; } - if(out_len != LIBSSH2_MLKEM_SHARED_SECRET_LEN) { + if(out_len != SSH2_MLKEM_SHARED_SECRET_LEN) { rc = -1; goto clean_exit; } @@ -2828,11 +2817,11 @@ clean_exit: #endif #if LIBSSH2_RSA -int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, size_t *signature_len) +int ssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, size_t *signature_len) { int ret = -1; unsigned char *sig = NULL; @@ -2848,12 +2837,12 @@ int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, } if(sig_len > 0) - sig = LIBSSH2_ALLOC(session, sig_len); + sig = SSH2_ALLOC(session, sig_len); #else unsigned int sig_len = 0; sig_len = RSA_size(rsactx); - sig = LIBSSH2_ALLOC(session, sig_len); + sig = SSH2_ALLOC(session, sig_len); #endif if(!sig) { @@ -2868,8 +2857,8 @@ int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, else if(hash_len == SHA512_DIGEST_LENGTH) md = EVP_sha512(); else { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unsupported hash digest length"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unsupported hash digest length"); } if(md) { @@ -2896,14 +2885,14 @@ int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, ret = RSA_sign(NID_sha512, hash, (unsigned int)hash_len, sig, &sig_len, rsactx); else { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unsupported hash digest length"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unsupported hash digest length"); ret = -1; } #endif if(!ret) { - LIBSSH2_FREE(session, sig); + SSH2_FREE(session, sig); return -1; } @@ -2914,22 +2903,22 @@ int _libssh2_rsa_sha2_sign(LIBSSH2_SESSION *session, } #if LIBSSH2_RSA_SHA1 -int _libssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, size_t *signature_len) +int ssh2_rsa_sha1_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, size_t *signature_len) { - return _libssh2_rsa_sha2_sign(session, rsactx, hash, hash_len, - signature, signature_len); + return ssh2_rsa_sha2_sign(session, rsactx, hash, hash_len, + signature, signature_len); } #endif #endif #if LIBSSH2_DSA -int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx *dsactx, - const unsigned char *hash, - size_t hash_len, unsigned char *signature) +int ssh2_dsa_sha1_sign(ssh2_dsa_ctx *dsactx, + const unsigned char *hash, + size_t hash_len, unsigned char *signature) { DSA_SIG *sig = NULL; const BIGNUM *r; @@ -3000,9 +2989,9 @@ int _libssh2_dsa_sha1_sign(libssh2_dsa_ctx *dsactx, #if LIBSSH2_ECDSA -int _libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx, - const unsigned char *hash, size_t hash_len, - unsigned char **signature, size_t *signature_len) +int ssh2_ecdsa_sign(LIBSSH2_SESSION *session, ssh2_ecdsa_ctx *ec_ctx, + const unsigned char *hash, size_t hash_len, + unsigned char **signature, size_t *signature_len) { int r_len, s_len; int rc = 0; @@ -3019,7 +3008,7 @@ int _libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx, rc = -1; if(!ctx) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "out of memory"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, "out of memory"); } out_buffer_len = EVP_PKEY_get_size(ec_ctx); @@ -3073,7 +3062,7 @@ int _libssh2_ecdsa_sign(LIBSSH2_SESSION *session, libssh2_ecdsa_ctx *ec_ctx, out_buffer_len = (size_t)(sp - temp_buffer); - out_buffer = LIBSSH2_CALLOC(session, out_buffer_len); + out_buffer = SSH2_CALLOC(session, out_buffer_len); if(!out_buffer) { rc = -1; goto clean_exit; @@ -3101,7 +3090,7 @@ clean_exit: } #endif /* LIBSSH2_ECDSA */ -int _libssh2_sha1_init(libssh2_sha1_ctx *ctx) +int ssh2_ossl_sha1_init(ssh2_sha1_ctx *ctx) { #ifdef HAVE_OPAQUE_STRUCTS *ctx = EVP_MD_CTX_new(); @@ -3122,7 +3111,7 @@ int _libssh2_sha1_init(libssh2_sha1_ctx *ctx) #endif } -int _libssh2_sha1_update(libssh2_sha1_ctx *ctx, const void *data, size_t len) +int ssh2_ossl_sha1_update(ssh2_sha1_ctx *ctx, const void *data, size_t len) { #ifdef HAVE_OPAQUE_STRUCTS return EVP_DigestUpdate(*ctx, data, len); @@ -3131,7 +3120,7 @@ int _libssh2_sha1_update(libssh2_sha1_ctx *ctx, const void *data, size_t len) #endif } -int _libssh2_sha1_final(libssh2_sha1_ctx *ctx, unsigned char *out) +int ssh2_ossl_sha1_final(ssh2_sha1_ctx *ctx, unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS int ret = EVP_DigestFinal(*ctx, out, NULL); @@ -3142,7 +3131,8 @@ int _libssh2_sha1_final(libssh2_sha1_ctx *ctx, unsigned char *out) #endif } -int _libssh2_sha1(const unsigned char *message, size_t len, unsigned char *out) +int ssh2_ossl_sha1(const unsigned char *message, size_t len, + unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS EVP_MD_CTX *ctx = EVP_MD_CTX_new(); @@ -3170,7 +3160,7 @@ int _libssh2_sha1(const unsigned char *message, size_t len, unsigned char *out) return 1; /* error */ } -int _libssh2_sha256_init(libssh2_sha256_ctx *ctx) +int ssh2_ossl_sha256_init(ssh2_sha256_ctx *ctx) { #ifdef HAVE_OPAQUE_STRUCTS *ctx = EVP_MD_CTX_new(); @@ -3191,8 +3181,7 @@ int _libssh2_sha256_init(libssh2_sha256_ctx *ctx) #endif } -int _libssh2_sha256_update(libssh2_sha256_ctx *ctx, - const void *data, size_t len) +int ssh2_ossl_sha256_update(ssh2_sha256_ctx *ctx, const void *data, size_t len) { #ifdef HAVE_OPAQUE_STRUCTS return EVP_DigestUpdate(*ctx, data, len); @@ -3201,7 +3190,7 @@ int _libssh2_sha256_update(libssh2_sha256_ctx *ctx, #endif } -int _libssh2_sha256_final(libssh2_sha256_ctx *ctx, unsigned char *out) +int ssh2_ossl_sha256_final(ssh2_sha256_ctx *ctx, unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS int ret = EVP_DigestFinal(*ctx, out, NULL); @@ -3212,8 +3201,8 @@ int _libssh2_sha256_final(libssh2_sha256_ctx *ctx, unsigned char *out) #endif } -int _libssh2_sha256(const unsigned char *message, size_t len, - unsigned char *out) +int ssh2_ossl_sha256(const unsigned char *message, size_t len, + unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS EVP_MD_CTX *ctx = EVP_MD_CTX_new(); @@ -3241,7 +3230,7 @@ int _libssh2_sha256(const unsigned char *message, size_t len, return 1; /* error */ } -int _libssh2_sha384_init(libssh2_sha384_ctx *ctx) +int ssh2_ossl_sha384_init(ssh2_sha384_ctx *ctx) { #ifdef HAVE_OPAQUE_STRUCTS *ctx = EVP_MD_CTX_new(); @@ -3262,8 +3251,7 @@ int _libssh2_sha384_init(libssh2_sha384_ctx *ctx) #endif } -int _libssh2_sha384_update(libssh2_sha384_ctx *ctx, - const void *data, size_t len) +int ssh2_ossl_sha384_update(ssh2_sha384_ctx *ctx, const void *data, size_t len) { #ifdef HAVE_OPAQUE_STRUCTS return EVP_DigestUpdate(*ctx, data, len); @@ -3272,7 +3260,7 @@ int _libssh2_sha384_update(libssh2_sha384_ctx *ctx, #endif } -int _libssh2_sha384_final(libssh2_sha384_ctx *ctx, unsigned char *out) +int ssh2_ossl_sha384_final(ssh2_sha384_ctx *ctx, unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS int ret = EVP_DigestFinal(*ctx, out, NULL); @@ -3283,8 +3271,8 @@ int _libssh2_sha384_final(libssh2_sha384_ctx *ctx, unsigned char *out) #endif } -int _libssh2_sha384(const unsigned char *message, size_t len, - unsigned char *out) +int ssh2_ossl_sha384(const unsigned char *message, size_t len, + unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS EVP_MD_CTX *ctx = EVP_MD_CTX_new(); @@ -3312,7 +3300,7 @@ int _libssh2_sha384(const unsigned char *message, size_t len, return 1; /* error */ } -int _libssh2_sha512_init(libssh2_sha512_ctx *ctx) +int ssh2_ossl_sha512_init(ssh2_sha512_ctx *ctx) { #ifdef HAVE_OPAQUE_STRUCTS *ctx = EVP_MD_CTX_new(); @@ -3333,8 +3321,7 @@ int _libssh2_sha512_init(libssh2_sha512_ctx *ctx) #endif } -int _libssh2_sha512_update(libssh2_sha512_ctx *ctx, - const void *data, size_t len) +int ssh2_ossl_sha512_update(ssh2_sha512_ctx *ctx, const void *data, size_t len) { #ifdef HAVE_OPAQUE_STRUCTS return EVP_DigestUpdate(*ctx, data, len); @@ -3343,7 +3330,7 @@ int _libssh2_sha512_update(libssh2_sha512_ctx *ctx, #endif } -int _libssh2_sha512_final(libssh2_sha512_ctx *ctx, unsigned char *out) +int ssh2_ossl_sha512_final(ssh2_sha512_ctx *ctx, unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS int ret = EVP_DigestFinal(*ctx, out, NULL); @@ -3354,8 +3341,8 @@ int _libssh2_sha512_final(libssh2_sha512_ctx *ctx, unsigned char *out) #endif } -int _libssh2_sha512(const unsigned char *message, size_t len, - unsigned char *out) +int ssh2_ossl_sha512(const unsigned char *message, size_t len, + unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS EVP_MD_CTX *ctx = EVP_MD_CTX_new(); @@ -3384,7 +3371,7 @@ int _libssh2_sha512(const unsigned char *message, size_t len, } #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM -int _libssh2_md5_init(libssh2_md5_ctx *ctx) +int ssh2_ossl_md5_init(ssh2_md5_ctx *ctx) { /* MD5 digest is not supported in OpenSSL FIPS mode * Trying to init it results in a latent OpenSSL error: @@ -3418,7 +3405,7 @@ int _libssh2_md5_init(libssh2_md5_ctx *ctx) #endif } -int _libssh2_md5_update(libssh2_md5_ctx *ctx, const void *data, size_t len) +int ssh2_ossl_md5_update(ssh2_md5_ctx *ctx, const void *data, size_t len) { #ifdef HAVE_OPAQUE_STRUCTS return EVP_DigestUpdate(*ctx, data, len); @@ -3427,7 +3414,7 @@ int _libssh2_md5_update(libssh2_md5_ctx *ctx, const void *data, size_t len) #endif } -int _libssh2_md5_final(libssh2_md5_ctx *ctx, unsigned char *out) +int ssh2_ossl_md5_final(ssh2_md5_ctx *ctx, unsigned char *out) { #ifdef HAVE_OPAQUE_STRUCTS int ret = EVP_DigestFinal(*ctx, out, NULL); @@ -3457,21 +3444,21 @@ static int gen_publickey_from_ec_evp(LIBSSH2_SESSION *session, size_t key_len = 0; unsigned char *octal_value = NULL; size_t octal_len; - libssh2_curve_type type; + ssh2_curve_type type; #ifdef USE_OPENSSL_3 - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from EC private key envelope")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from EC private key envelope")); - type = _libssh2_ecdsa_get_curve_type(pk); + type = ssh2_ecdsa_get_curve_type(pk); #else EC_KEY *ec = NULL; const EC_POINT *public_key; const EC_GROUP *group; BN_CTX *bn_ctx = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from EC private key envelope")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from EC private key envelope")); bn_ctx = BN_CTX_new(); if(!bn_ctx) @@ -3485,7 +3472,7 @@ static int gen_publickey_from_ec_evp(LIBSSH2_SESSION *session, public_key = EC_KEY_get0_public_key(ec); group = EC_KEY_get0_group(ec); - type = _libssh2_ecdsa_get_curve_type(ec); + type = ssh2_ecdsa_get_curve_type(ec); #endif if(is_sk) @@ -3493,34 +3480,34 @@ static int gen_publickey_from_ec_evp(LIBSSH2_SESSION *session, else method_buf_len = 19; - method_buf = LIBSSH2_ALLOC(session, method_buf_len); + method_buf = SSH2_ALLOC(session, method_buf_len); if(!method_buf) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "out of memory"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, "out of memory"); } if(is_sk) { memcpy(method_buf, "sk-ecdsa-sha2-nistp256@openssh.com", method_buf_len); } - else if(type == LIBSSH2_EC_CURVE_NISTP256) { + else if(type == SSH2_EC_CURVE_NISTP256) { memcpy(method_buf, "ecdsa-sha2-nistp256", method_buf_len); } - else if(type == LIBSSH2_EC_CURVE_NISTP384) { + else if(type == SSH2_EC_CURVE_NISTP384) { memcpy(method_buf, "ecdsa-sha2-nistp384", method_buf_len); } - else if(type == LIBSSH2_EC_CURVE_NISTP521) { + else if(type == SSH2_EC_CURVE_NISTP521) { memcpy(method_buf, "ecdsa-sha2-nistp521", method_buf_len); } else { - _libssh2_debug((session, LIBSSH2_TRACE_ERROR, - "Unsupported EC private key type")); + ssh2_deb((session, LIBSSH2_TRACE_ERROR, + "Unsupported EC private key type")); rc = -1; goto clean_exit; } #ifdef USE_OPENSSL_3 octal_len = EC_MAX_POINT_LEN; - octal_value = LIBSSH2_ALLOC(session, octal_len); + octal_value = SSH2_ALLOC(session, octal_len); EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY, octal_value, octal_len, &octal_len); #else @@ -3550,7 +3537,7 @@ static int gen_publickey_from_ec_evp(LIBSSH2_SESSION *session, /* Key form is: type_len(4) + type(method_buf_len) + domain_len(4) + domain(8) + pub_key_len(4) + pub_key(~65). */ key_len = 4 + method_buf_len + 4 + 8 + 4 + octal_len; - key = LIBSSH2_ALLOC(session, key_len); + key = SSH2_ALLOC(session, key_len); if(!key) { rc = -1; goto clean_exit; @@ -3560,18 +3547,18 @@ static int gen_publickey_from_ec_evp(LIBSSH2_SESSION *session, p = key; /* Key type */ - _libssh2_store_str(&p, (const char *)method_buf, method_buf_len); + ssh2_store_str(&p, (const char *)method_buf, method_buf_len); /* Name domain */ if(is_sk) { - _libssh2_store_str(&p, "nistp256", 8); + ssh2_store_str(&p, "nistp256", 8); } else { - _libssh2_store_str(&p, (const char *)method_buf + 11, 8); + ssh2_store_str(&p, (const char *)method_buf + 11, 8); } /* Public key */ - _libssh2_store_str(&p, (const char *)octal_value, octal_len); + ssh2_store_str(&p, (const char *)octal_value, octal_len); *method = method_buf; if(method_len) { @@ -3600,25 +3587,25 @@ clean_exit: return 0; if(method_buf) - LIBSSH2_FREE(session, method_buf); + SSH2_FREE(session, method_buf); return -1; } static int gen_publickey_from_ecdsa_openssh_priv_data( LIBSSH2_SESSION *session, - libssh2_curve_type curve_type, + ssh2_curve_type curve_type, struct string_buf *decrypted, unsigned char **method, size_t *method_len, unsigned char **pubkeydata, size_t *pubkeydata_len, - libssh2_ecdsa_ctx **ec_ctx) + ssh2_ecdsa_ctx **ec_ctx) { int rc = 0; size_t curvelen, exponentlen, pointlen; unsigned char *curve, *exponent, *point_buf; - libssh2_ecdsa_ctx *ec_key = NULL; + ssh2_ecdsa_ctx *ec_key = NULL; #ifdef USE_OPENSSL_3 EVP_PKEY_CTX *fromdata_ctx = NULL; @@ -3629,23 +3616,21 @@ static int gen_publickey_from_ecdsa_openssh_priv_data( BIGNUM *bn_exponent; #endif - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing ECDSA keys from private key data")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing ECDSA keys from private key data")); - if(_libssh2_get_string(decrypted, &curve, &curvelen) || - curvelen == 0) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "ECDSA no curve"); + if(ssh2_get_string(decrypted, &curve, &curvelen) || curvelen == 0) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "ECDSA no curve"); return -1; } - if(_libssh2_get_string(decrypted, &point_buf, &pointlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "ECDSA no point"); + if(ssh2_get_string(decrypted, &point_buf, &pointlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "ECDSA no point"); return -1; } - if(_libssh2_get_bignum_bytes(decrypted, &exponent, &exponentlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "ECDSA no exponent"); + if(ssh2_get_bignum_bytes(decrypted, &exponent, &exponentlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "ECDSA no exponent"); return -1; } @@ -3687,21 +3672,19 @@ static int gen_publickey_from_ecdsa_openssh_priv_data( if(group_name) OPENSSL_clear_free(group_name, strlen(n)); #else - rc = _libssh2_ecdsa_curve_name_with_octal_new(&ec_key, - point_buf, pointlen, - curve_type); + rc = ssh2_ecdsa_curve_name_with_octal_new(&ec_key, point_buf, pointlen, + curve_type); if(rc) { rc = -1; - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "ECDSA could not create key"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "ECDSA could not create key"); goto fail; } bn_exponent = BN_new(); if(!bn_exponent) { rc = -1; - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for private key data"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for private key data"); goto fail; } @@ -3735,7 +3718,7 @@ static int gen_publickey_from_ecdsa_openssh_priv_data( if(ec_ctx) *ec_ctx = ec_key; else - _libssh2_ecdsa_free(ec_key); + ssh2_ecdsa_free(ec_key); return rc; @@ -3746,7 +3729,7 @@ fail: #endif if(ec_key) - _libssh2_ecdsa_free(ec_key); + ssh2_ecdsa_free(ec_key); return rc; } @@ -3762,60 +3745,55 @@ static int gen_publickey_from_sk_ecdsa_openssh_priv_data( const char **application, const unsigned char **key_handle, size_t *handle_len, - libssh2_ecdsa_ctx **ec_ctx) + ssh2_ecdsa_ctx **ec_ctx) { int rc = 0; size_t curvelen, pointlen, key_len, app_len; unsigned char *curve, *point_buf, *p, *key, *app; - libssh2_ecdsa_ctx *ec_key = NULL; + ssh2_ecdsa_ctx *ec_key = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Extracting ECDSA-SK public key")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, "Extracting ECDSA-SK public key")); - if(_libssh2_get_string(decrypted, &curve, &curvelen) || - curvelen == 0) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "ECDSA no curve"); + if(ssh2_get_string(decrypted, &curve, &curvelen) || curvelen == 0) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "ECDSA no curve"); return -1; } - if(_libssh2_get_string(decrypted, &point_buf, &pointlen)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "ECDSA no point"); + if(ssh2_get_string(decrypted, &point_buf, &pointlen)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "ECDSA no point"); return -1; } - rc = _libssh2_ecdsa_curve_name_with_octal_new(&ec_key, - point_buf, pointlen, - LIBSSH2_EC_CURVE_NISTP256); + rc = ssh2_ecdsa_curve_name_with_octal_new(&ec_key, point_buf, pointlen, + SSH2_EC_CURVE_NISTP256); if(rc) { rc = -1; - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "ECDSA could not create key"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "ECDSA could not create key"); goto fail; } - if(_libssh2_get_string(decrypted, &app, &app_len)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "No SK application."); + if(ssh2_get_string(decrypted, &app, &app_len)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "No SK application."); goto fail; } - if(flags && _libssh2_get_byte(decrypted, flags)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "No SK flags."); + if(flags && ssh2_get_byte(decrypted, flags)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "No SK flags."); goto fail; } if(key_handle && handle_len) { unsigned char *handle = NULL; - if(_libssh2_get_string(decrypted, &handle, handle_len)) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "No SK key_handle."); + if(ssh2_get_string(decrypted, &handle, handle_len)) { + ssh2_err(session, LIBSSH2_ERROR_PROTO, "No SK key_handle."); goto fail; } if(*handle_len > 0) { - *key_handle = LIBSSH2_ALLOC(session, *handle_len); + *key_handle = SSH2_ALLOC(session, *handle_len); if(*key_handle) { - memcpy((void *)LIBSSH2_UNCONST(*key_handle), - handle, *handle_len); + memcpy(SSH2_UNCONST(*key_handle), handle, *handle_len); } } } @@ -3840,7 +3818,7 @@ static int gen_publickey_from_sk_ecdsa_openssh_priv_data( if(rc == 0 && pubkeydata) { key_len = *pubkeydata_len + app_len + 4; - key = LIBSSH2_ALLOC(session, key_len); + key = SSH2_ALLOC(session, key_len); if(!key) { rc = -1; @@ -3850,16 +3828,15 @@ static int gen_publickey_from_sk_ecdsa_openssh_priv_data( p = key + *pubkeydata_len; memcpy(key, *pubkeydata, *pubkeydata_len); - _libssh2_store_str(&p, (const char *)app, app_len); + ssh2_store_str(&p, (const char *)app, app_len); if(application && app_len > 0) { - *application = LIBSSH2_ALLOC(session, app_len + 1); - _libssh2_explicit_zero((void *)LIBSSH2_UNCONST(*application), - app_len + 1); - memcpy((void *)LIBSSH2_UNCONST(*application), app, app_len); + *application = SSH2_ALLOC(session, app_len + 1); + ssh2_explicit_zero(SSH2_UNCONST(*application), app_len + 1); + memcpy(SSH2_UNCONST(*application), app, app_len); } - LIBSSH2_FREE(session, *pubkeydata); + SSH2_FREE(session, *pubkeydata); *pubkeydata_len = key_len; *pubkeydata = key; } @@ -3867,28 +3844,28 @@ static int gen_publickey_from_sk_ecdsa_openssh_priv_data( if(ec_ctx) *ec_ctx = ec_key; else - _libssh2_ecdsa_free(ec_key); + ssh2_ecdsa_free(ec_key); return rc; fail: if(ec_key) - _libssh2_ecdsa_free(ec_key); + ssh2_ecdsa_free(ec_key); if(application && *application) { - LIBSSH2_FREE(session, (void *)application); + SSH2_FREE(session, (void *)application); *application = NULL; } if(key_handle && *key_handle) { - LIBSSH2_FREE(session, (void *)key_handle); + SSH2_FREE(session, (void *)key_handle); *key_handle = NULL; } return rc; } -static int ecdsa_new_openssh_private(libssh2_ecdsa_ctx **ec_ctx, +static int ecdsa_new_openssh_private(ssh2_ecdsa_ctx **ec_ctx, LIBSSH2_SESSION *session, const char *filename, const unsigned char *passphrase) @@ -3896,39 +3873,39 @@ static int ecdsa_new_openssh_private(libssh2_ecdsa_ctx **ec_ctx, FILE *fp; int rc; unsigned char *buf = NULL; - libssh2_curve_type type; + ssh2_curve_type type; struct string_buf *decrypted = NULL; if(!session) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Session is required"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); return -1; } - _libssh2_init_if_needed(); + ssh2_init_if_needed(); fp = fopen(filename, "r"); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open OpenSSH ECDSA private key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open OpenSSH ECDSA private key file"); return -1; } - rc = _libssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); + rc = ssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); fclose(fp); if(rc) { return rc; } /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted key data not found"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); return -1; } - rc = _libssh2_ecdsa_curve_type_from_name((const char *)buf, &type); + rc = ssh2_ecdsa_curve_type_from_name((const char *)buf, &type); if(rc == 0) { rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type, @@ -3941,12 +3918,12 @@ static int ecdsa_new_openssh_private(libssh2_ecdsa_ctx **ec_ctx, } if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); return rc; } -static int ecdsa_new_openssh_private_sk(libssh2_ecdsa_ctx **ec_ctx, +static int ecdsa_new_openssh_private_sk(ssh2_ecdsa_ctx **ec_ctx, uint8_t *flags, const char **application, const unsigned char **key_handle, @@ -3961,31 +3938,31 @@ static int ecdsa_new_openssh_private_sk(libssh2_ecdsa_ctx **ec_ctx, struct string_buf *decrypted = NULL; if(!session) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Session is required"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); return -1; } - _libssh2_init_if_needed(); + ssh2_init_if_needed(); fp = fopen(filename, "r"); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open OpenSSH ECDSA private key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open OpenSSH ECDSA private key file"); return -1; } - rc = _libssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); + rc = ssh2_openssh_pem_parse(session, passphrase, fp, &decrypted); fclose(fp); if(rc) { return rc; } /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted key data not found"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); return -1; } @@ -4005,15 +3982,15 @@ static int ecdsa_new_openssh_private_sk(libssh2_ecdsa_ctx **ec_ctx, } if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); return rc; } -int _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx **ec_ctx, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_ecdsa_new_private(ssh2_ecdsa_ctx **ec_ctx, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { int rc; @@ -4025,7 +4002,7 @@ int _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx **ec_ctx, (pem_read_bio_func)&PEM_read_bio_ECPrivateKey; #endif - _libssh2_init_if_needed(); + ssh2_init_if_needed(); rc = read_private_key_from_file((void **)ec_ctx, read_ec, filename, passphrase); @@ -4038,14 +4015,14 @@ int _libssh2_ecdsa_new_private(libssh2_ecdsa_ctx **ec_ctx, return rc; } -int _libssh2_ecdsa_new_private_sk(libssh2_ecdsa_ctx **ec_ctx, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_ecdsa_new_private_sk(ssh2_ecdsa_ctx **ec_ctx, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { int rc; @@ -4057,7 +4034,7 @@ int _libssh2_ecdsa_new_private_sk(libssh2_ecdsa_ctx **ec_ctx, (pem_read_bio_func)&PEM_read_bio_ECPrivateKey; #endif - _libssh2_init_if_needed(); + ssh2_init_if_needed(); rc = read_private_key_from_file((void **)ec_ctx, read_ec, filename, passphrase); @@ -4080,16 +4057,16 @@ int _libssh2_ecdsa_new_private_sk(libssh2_ecdsa_ctx **ec_ctx, * Creates a local private key based on input curve * and returns octal value and octal length */ -int _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, - libssh2_ec_key **out_private_key, - unsigned char **out_public_key_octal, - size_t *out_public_key_octal_len, - libssh2_curve_type curve_type) +int ssh2_ecdsa_create_key(LIBSSH2_SESSION *session, + ssh2_ec_key **out_private_key, + unsigned char **out_public_key_octal, + size_t *out_public_key_octal_len, + ssh2_curve_type curve_type) { int ret = 1; size_t octal_len = 0; unsigned char octal_value[EC_MAX_POINT_LEN]; - libssh2_ec_key *private_key = NULL; + ssh2_ec_key *private_key = NULL; #ifdef USE_OPENSSL_3 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL); @@ -4114,7 +4091,7 @@ int _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, goto clean_exit; } - *out_public_key_octal = LIBSSH2_ALLOC(session, octal_len); + *out_public_key_octal = SSH2_ALLOC(session, octal_len); if(!(*out_public_key_octal)) { ret = -1; @@ -4167,7 +4144,7 @@ int _libssh2_ecdsa_create_key(LIBSSH2_SESSION *session, *out_private_key = private_key; if(out_public_key_octal) { - *out_public_key_octal = LIBSSH2_ALLOC(session, octal_len); + *out_public_key_octal = SSH2_ALLOC(session, octal_len); if(!*out_public_key_octal) { ret = -1; goto clean_exit; @@ -4196,9 +4173,9 @@ clean_exit: * Computes the shared secret K given a local private key, * remote public key and length */ -int _libssh2_ecdh_gen_k(libssh2_bn **k, libssh2_ec_key *private_key, - const unsigned char *server_public_key, - size_t server_public_key_len) +int ssh2_ecdh_gen_k(ssh2_bn **k, ssh2_ec_key *private_key, + const unsigned char *server_public_key, + size_t server_public_key_len) { int ret = 0; BN_CTX *bn_ctx = NULL; @@ -4381,9 +4358,9 @@ clean_exit: #if LIBSSH2_ED25519 -int _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, - uint8_t **out_sig, size_t *out_sig_len, - const uint8_t *message, size_t message_len) +int ssh2_ed25519_sign(ssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, + uint8_t **out_sig, size_t *out_sig_len, + const uint8_t *message, size_t message_len) { int rc = -1; EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); @@ -4396,10 +4373,10 @@ int _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, if(EVP_DigestSign(md_ctx, NULL, &sig_len, message, message_len) != 1) goto clean_exit; - if(sig_len != LIBSSH2_ED25519_SIG_LEN) + if(sig_len != SSH2_ED25519_SIG_LEN) goto clean_exit; - sig = LIBSSH2_CALLOC(session, sig_len); + sig = SSH2_CALLOC(session, sig_len); if(!sig) goto clean_exit; @@ -4413,7 +4390,7 @@ int _libssh2_ed25519_sign(libssh2_ed25519_ctx *ctx, LIBSSH2_SESSION *session, else { *out_sig_len = 0; *out_sig = NULL; - LIBSSH2_FREE(session, sig); + SSH2_FREE(session, sig); } clean_exit: @@ -4424,13 +4401,12 @@ clean_exit: return (rc == 1) ? 0 : -1; } -int _libssh2_curve25519_gen_k( - libssh2_bn **k, - uint8_t private_key[LIBSSH2_ED25519_KEY_LEN], - uint8_t server_public_key[LIBSSH2_ED25519_KEY_LEN]) +int ssh2_curve25519_gen_k(ssh2_bn **k, + uint8_t private_key[SSH2_ED25519_KEY_LEN], + uint8_t server_public_key[SSH2_ED25519_KEY_LEN]) { int rc = -1; - unsigned char out_shared_key[LIBSSH2_ED25519_KEY_LEN]; + unsigned char out_shared_key[SSH2_ED25519_KEY_LEN]; EVP_PKEY *peer_key = NULL, *server_key = NULL; EVP_PKEY_CTX *server_key_ctx = NULL; BN_CTX *bn_ctx = NULL; @@ -4445,11 +4421,11 @@ int _libssh2_curve25519_gen_k( peer_key = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, server_public_key, - LIBSSH2_ED25519_KEY_LEN); + SSH2_ED25519_KEY_LEN); server_key = EVP_PKEY_new_raw_private_key(EVP_PKEY_X25519, NULL, private_key, - LIBSSH2_ED25519_KEY_LEN); + SSH2_ED25519_KEY_LEN); if(!peer_key || !server_key) { goto clean_exit; @@ -4475,15 +4451,15 @@ int _libssh2_curve25519_gen_k( goto clean_exit; } - if(out_len != LIBSSH2_ED25519_KEY_LEN) { + if(out_len != SSH2_ED25519_KEY_LEN) { rc = -1; goto clean_exit; } rc = EVP_PKEY_derive(server_key_ctx, out_shared_key, &out_len); - if(rc == 1 && out_len == LIBSSH2_ED25519_KEY_LEN) { - BN_bin2bn(out_shared_key, LIBSSH2_ED25519_KEY_LEN, *k); + if(rc == 1 && out_len == SSH2_ED25519_KEY_LEN) { + BN_bin2bn(out_shared_key, SSH2_ED25519_KEY_LEN, *k); } else { rc = -1; @@ -4503,8 +4479,8 @@ clean_exit: return (rc == 1) ? 0 : -1; } -int _libssh2_ed25519_verify(libssh2_ed25519_ctx *ctx, const uint8_t *s, - size_t s_len, const uint8_t *m, size_t m_len) +int ssh2_ed25519_verify(ssh2_ed25519_ctx *ctx, const uint8_t *s, + size_t s_len, const uint8_t *m, size_t m_len) { int ret = -1; @@ -4541,33 +4517,33 @@ static int pub_priv_openssh_keyfile(LIBSSH2_SESSION *session, int rc = 0; if(!session) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, "Session is required"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); return -1; } - _libssh2_init_if_needed(); + ssh2_init_if_needed(); fp = fopen(privatekey, "r"); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open private key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open private key file"); return -1; } - rc = _libssh2_openssh_pem_parse(session, (const unsigned char *)passphrase, - fp, &decrypted); + rc = ssh2_openssh_pem_parse(session, (const unsigned char *)passphrase, + fp, &decrypted); fclose(fp); if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, "Not an OpenSSH key file"); + ssh2_err(session, LIBSSH2_ERROR_FILE, "Not an OpenSSH key file"); return rc; } /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted key data not found"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); return -1; } @@ -4606,10 +4582,9 @@ static int pub_priv_openssh_keyfile(LIBSSH2_SESSION *session, #endif #if LIBSSH2_ECDSA { - libssh2_curve_type type; + ssh2_curve_type type; - if(_libssh2_ecdsa_curve_type_from_name((const char *)buf, - &type) == 0) { + if(ssh2_ecdsa_curve_type_from_name((const char *)buf, &type) == 0) { rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type, decrypted, method, method_len, @@ -4621,23 +4596,22 @@ static int pub_priv_openssh_keyfile(LIBSSH2_SESSION *session, #endif if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unsupported OpenSSH key type"); + ssh2_err(session, LIBSSH2_ERROR_FILE, "Unsupported OpenSSH key type"); } return rc; } -int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekey, - const char *passphrase) +int ssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekey, + const char *passphrase) { int st; BIO *bp; @@ -4645,20 +4619,18 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, int pktype; int rc; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from private key file: %s", - privatekey)); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from private key file: %s", privatekey)); bp = BIO_new_file(privatekey, "r"); if(!bp) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key from private key " - "file: Unable to open private key file"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key from private key file: " + "Unable to open private key file"); } (void)BIO_reset(bp); - pk = PEM_read_bio_PrivateKey(bp, NULL, NULL, - (void *)LIBSSH2_UNCONST(passphrase)); + pk = PEM_read_bio_PrivateKey(bp, NULL, NULL, SSH2_UNCONST(passphrase)); BIO_free(bp); if(!pk) { @@ -4670,11 +4642,11 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, pubkeydata, pubkeydata_len, privatekey, passphrase); if(rc) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key " - "from private key file: " - "Wrong passphrase or invalid/unrecognized " - "private key file format"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key " + "from private key file: " + "Wrong passphrase or invalid/unrecognized " + "private key file format"); } return 0; @@ -4712,10 +4684,9 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, break; #endif /* LIBSSH2_ECDSA */ default: - st = _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key " - "from private key file: " - "Unsupported private key file format"); + st = ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key from private key file: " + "Unsupported private key file format"); break; } @@ -4742,28 +4713,26 @@ static int pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session, *key_ctx = NULL; if(!session) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Session is required"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); if(key_type && (strlen(key_type) > 11 || strlen(key_type) < 7)) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, "type is invalid"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, "type is invalid"); - _libssh2_init_if_needed(); + ssh2_init_if_needed(); - rc = _libssh2_openssh_pem_parse_memory(session, passphrase, - privatekeydata, - privatekeydata_len, &decrypted); + rc = ssh2_openssh_pem_parse_memory(session, passphrase, + privatekeydata, + privatekeydata_len, &decrypted); if(rc) return rc; /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted " - "key data not found"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); rc = LIBSSH2_ERROR_FILE; @@ -4782,7 +4751,7 @@ static int pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session, method_len, pubkeydata, pubkeydata_len, - (libssh2_ed25519_ctx **)key_ctx); + (ssh2_ed25519_ctx **)key_ctx); } } @@ -4797,7 +4766,7 @@ static int pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session, pubkeydata_len, NULL, NULL, NULL, NULL, - (libssh2_ed25519_ctx **)key_ctx); + (ssh2_ed25519_ctx **)key_ctx); } } #endif @@ -4808,7 +4777,7 @@ static int pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session, method, method_len, pubkeydata, pubkeydata_len, - (libssh2_rsa_ctx **)key_ctx); + (ssh2_rsa_ctx **)key_ctx); } } #endif @@ -4819,13 +4788,13 @@ static int pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session, method, method_len, pubkeydata, pubkeydata_len, - (libssh2_dsa_ctx **)key_ctx); + (ssh2_dsa_ctx **)key_ctx); } } #endif #if LIBSSH2_ECDSA { - libssh2_curve_type type; + ssh2_curve_type type; if(strcmp("sk-ecdsa-sha2-nistp256@openssh.com", (const char *)buf) == 0) { @@ -4837,9 +4806,9 @@ static int pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session, pubkeydata_len, NULL, NULL, NULL, NULL, - (libssh2_ecdsa_ctx **)key_ctx); + (ssh2_ecdsa_ctx **)key_ctx); } - else if(_libssh2_ecdsa_curve_type_from_name((const char *)buf, &type) + else if(ssh2_ecdsa_curve_type_from_name((const char *)buf, &type) == 0) { if(!key_type || strcmp("ssh-ecdsa", key_type) == 0) { rc = gen_publickey_from_ecdsa_openssh_priv_data(session, type, @@ -4848,20 +4817,19 @@ static int pub_priv_openssh_keyfilememory(LIBSSH2_SESSION *session, method_len, pubkeydata, pubkeydata_len, - (libssh2_ecdsa_ctx **)key_ctx); + (ssh2_ecdsa_ctx **)key_ctx); } } } #endif if(rc == LIBSSH2_ERROR_FILE) - rc = _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key from private key " - "file: invalid/unrecognized private key " - "file format"); + rc = ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key from private key file: " + "invalid/unrecognized private key file format"); if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); return rc; } @@ -4890,28 +4858,26 @@ static int sk_pub_openssh_keyfilememory(LIBSSH2_SESSION *session, *key_ctx = NULL; if(!session) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Session is required"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, "Session is required"); if(key_type && strlen(key_type) < 7) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, "type is invalid"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, "type is invalid"); - _libssh2_init_if_needed(); + ssh2_init_if_needed(); - rc = _libssh2_openssh_pem_parse_memory(session, passphrase, - privatekeydata, - privatekeydata_len, &decrypted); + rc = ssh2_openssh_pem_parse_memory(session, passphrase, + privatekeydata, + privatekeydata_len, &decrypted); if(rc) return rc; /* We have a new key file, now try and parse it using supported types */ - rc = _libssh2_get_string(decrypted, &buf, NULL); + rc = ssh2_get_string(decrypted, &buf, NULL); if(rc || !buf) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Public key type in decrypted " - "key data not found"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Public key type in decrypted key data not found"); rc = LIBSSH2_ERROR_FILE; @@ -4941,7 +4907,7 @@ static int sk_pub_openssh_keyfilememory(LIBSSH2_SESSION *session, application, key_handle, handle_len, - (libssh2_ed25519_ctx **)key_ctx); + (ssh2_ed25519_ctx **)key_ctx); } } #endif @@ -4956,18 +4922,18 @@ static int sk_pub_openssh_keyfilememory(LIBSSH2_SESSION *session, application, key_handle, handle_len, - (libssh2_ecdsa_ctx **)key_ctx); + (ssh2_ecdsa_ctx **)key_ctx); } #endif if(rc == LIBSSH2_ERROR_FILE) - rc = _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key from private key " - "file: invalid/unrecognized private key " - "file format"); + rc = ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key from private key " + "file: invalid/unrecognized private key " + "file format"); if(decrypted) - _libssh2_string_buf_free(session, decrypted); + ssh2_string_buf_free(session, decrypted); return rc; } @@ -4976,14 +4942,14 @@ static int sk_pub_openssh_keyfilememory(LIBSSH2_SESSION *session, #define HAVE_SSLERROR_BAD_DECRYPT #endif -int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { int st; BIO *bp; @@ -4993,17 +4959,16 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, unsigned long sslError; #endif - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from private key.")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from private key.")); bp = BIO_new_mem_buf(privatekeydata, (int)privatekeydata_len); if(!bp) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory when" - "computing public key"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory when" + "computing public key"); (void)BIO_reset(bp); - pk = PEM_read_bio_PrivateKey(bp, NULL, NULL, - (void *)LIBSSH2_UNCONST(passphrase)); + pk = PEM_read_bio_PrivateKey(bp, NULL, NULL, SSH2_UNCONST(passphrase)); #ifdef HAVE_SSLERROR_BAD_DECRYPT sslError = ERR_get_error(); #endif @@ -5011,13 +4976,14 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, if(!pk) { /* Try OpenSSH format */ - st = pub_priv_openssh_keyfilememory(session, NULL, NULL, - method, - method_len, - pubkeydata, - pubkeydata_len, - privatekeydata, - privatekeydata_len, + st = pub_priv_openssh_keyfilememory(session, + NULL, NULL, + method, + method_len, + pubkeydata, + pubkeydata_len, + privatekeydata, + privatekeydata_len, (const unsigned char *)passphrase); if(st == 0) return 0; @@ -5027,13 +4993,13 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, ERR_GET_REASON(sslError) == PEM_R_BAD_DECRYPT) || (ERR_GET_LIB(sslError) == ERR_LIB_PROV && ERR_GET_REASON(sslError) == EVP_R_BAD_DECRYPT)) - return _libssh2_error(session, LIBSSH2_ERROR_KEYFILE_AUTH_FAILED, - "Wrong passphrase for private key"); + return ssh2_err(session, LIBSSH2_ERROR_KEYFILE_AUTH_FAILED, + "Wrong passphrase for private key"); #endif - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key " - "from private key file: " - "Unsupported private key file format"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key " + "from private key file: " + "Unsupported private key file format"); } #ifdef HAVE_OPAQUE_STRUCTS @@ -5068,10 +5034,10 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, break; #endif /* LIBSSH2_ECDSA */ default: - st = _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key " - "from private key file: " - "Unsupported private key file format"); + st = ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key " + "from private key file: " + "Unsupported private key file format"); break; } @@ -5079,35 +5045,34 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, return st; } -int _libssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - int *algorithm, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + int *algorithm, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { int st = -1; BIO *bp; EVP_PKEY *pk; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Computing public key from private key.")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Computing public key from private key.")); bp = BIO_new_mem_buf(privatekeydata, (int)privatekeydata_len); if(!bp) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory when" - "computing public key"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory when" + "computing public key"); (void)BIO_reset(bp); - pk = PEM_read_bio_PrivateKey(bp, NULL, NULL, - (void *)LIBSSH2_UNCONST(passphrase)); + pk = PEM_read_bio_PrivateKey(bp, NULL, NULL, SSH2_UNCONST(passphrase)); BIO_free(bp); if(!pk) { @@ -5130,14 +5095,14 @@ int _libssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, return st; } -void _libssh2_dh_init(libssh2_dh_ctx *dhctx) +void ssh2_ossl_dh_init(ssh2_dh_ctx *dhctx) { *dhctx = BN_new(); /* Random from client */ } -int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, int group_order, - libssh2_bn_ctx *bnctx) +int ssh2_ossl_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, int group_order, + ssh2_bn_ctx *bnctx) { /* Generate x and e */ BN_rand(*dhctx, group_order * 8 - 1, 0, -1); @@ -5145,22 +5110,22 @@ int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, return 0; } -int _libssh2_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p, - libssh2_bn_ctx *bnctx) +int ssh2_ossl_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p, + ssh2_bn_ctx *bnctx) { /* Compute the shared secret */ BN_mod_exp(secret, f, *dhctx, p, bnctx); return 0; } -void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx) +void ssh2_ossl_dh_dtor(ssh2_dh_ctx *dhctx) { BN_clear_free(*dhctx); *dhctx = NULL; } -int _libssh2_bn_from_bin(libssh2_bn *bn, size_t len, const unsigned char *val) +int ssh2_bn_from_bin(ssh2_bn *bn, size_t len, const unsigned char *val) { if(!BN_bin2bn(val, (int)len, bn)) { return -1; @@ -5172,9 +5137,9 @@ int _libssh2_bn_from_bin(libssh2_bn *bn, size_t len, const unsigned char *val) /* * Return supported key hash algo upgrades, see crypto.h */ -const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session, - unsigned char *key_method, - size_t key_method_len) +const char *ssh2_supported_key_sign_algs(LIBSSH2_SESSION *session, + unsigned char *key_method, + size_t key_method_len) { (void)session; diff --git a/src/openssl.h b/src/openssl.h index 258a366c..d709f870 100644 --- a/src/openssl.h +++ b/src/openssl.h @@ -40,7 +40,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -#define LIBSSH2_CRYPTO_ENGINE libssh2_openssl +#define SSH2_CRYPTO_ENGINE libssh2_openssl /* disable deprecated warnings in OpenSSL 3 */ #define OPENSSL_SUPPRESS_DEPRECATED @@ -225,125 +225,122 @@ #define EC_MAX_POINT_LEN ((528 * 2 / 8) + 1) -#define _libssh2_random(buf, len) _libssh2_openssl_random(buf, len) +#define ssh2_random(buf, len) ssh2_ossl_random(buf, len) -#define libssh2_prepare_iovec(vec, len) /* Empty. */ +#define ssh2_prepare_iovec(vec, len) /* Empty. */ #ifdef HAVE_OPAQUE_STRUCTS -#define libssh2_sha1_ctx EVP_MD_CTX * +#define ssh2_sha1_ctx EVP_MD_CTX * #else -#define libssh2_sha1_ctx EVP_MD_CTX +#define ssh2_sha1_ctx EVP_MD_CTX #endif /* returns 0 in case of failure */ -int _libssh2_sha1_init(libssh2_sha1_ctx *ctx); -int _libssh2_sha1_update(libssh2_sha1_ctx *ctx, - const void *data, size_t len); -int _libssh2_sha1_final(libssh2_sha1_ctx *ctx, unsigned char *out); -int _libssh2_sha1(const unsigned char *message, size_t len, - unsigned char *out); -#define libssh2_sha1_init(x) _libssh2_sha1_init(x) -#define libssh2_sha1_update(ctx, data, len) \ - _libssh2_sha1_update(&(ctx), data, len) -#define libssh2_sha1_final(ctx, out) _libssh2_sha1_final(&(ctx), out) -#define libssh2_sha1(x, y, z) _libssh2_sha1(x, y, z) +int ssh2_ossl_sha1_init(ssh2_sha1_ctx *ctx); +int ssh2_ossl_sha1_update(ssh2_sha1_ctx *ctx, const void *data, size_t len); +int ssh2_ossl_sha1_final(ssh2_sha1_ctx *ctx, unsigned char *out); +int ssh2_ossl_sha1(const unsigned char *message, size_t len, + unsigned char *out); +#define ssh2_sha1_init(x) ssh2_ossl_sha1_init(x) +#define ssh2_sha1_update(ctx, data, len) \ + ssh2_ossl_sha1_update(&(ctx), data, len) +#define ssh2_sha1_final(ctx, out) ssh2_ossl_sha1_final(&(ctx), out) +#define ssh2_sha1(x, y, z) ssh2_ossl_sha1(x, y, z) #ifdef HAVE_OPAQUE_STRUCTS -#define libssh2_sha256_ctx EVP_MD_CTX * +#define ssh2_sha256_ctx EVP_MD_CTX * #else -#define libssh2_sha256_ctx EVP_MD_CTX +#define ssh2_sha256_ctx EVP_MD_CTX #endif /* returns 0 in case of failure */ -int _libssh2_sha256_init(libssh2_sha256_ctx *ctx); -int _libssh2_sha256_update(libssh2_sha256_ctx *ctx, - const void *data, size_t len); -int _libssh2_sha256_final(libssh2_sha256_ctx *ctx, unsigned char *out); -int _libssh2_sha256(const unsigned char *message, size_t len, - unsigned char *out); -#define libssh2_sha256_init(x) _libssh2_sha256_init(x) -#define libssh2_sha256_update(ctx, data, len) \ - _libssh2_sha256_update(&(ctx), data, len) -#define libssh2_sha256_final(ctx, out) _libssh2_sha256_final(&(ctx), out) -#define libssh2_sha256(x, y, z) _libssh2_sha256(x, y, z) +int ssh2_ossl_sha256_init(ssh2_sha256_ctx *ctx); +int ssh2_ossl_sha256_update(ssh2_sha256_ctx *ctx, const void *data, + size_t len); +int ssh2_ossl_sha256_final(ssh2_sha256_ctx *ctx, unsigned char *out); +int ssh2_ossl_sha256(const unsigned char *message, size_t len, + unsigned char *out); +#define ssh2_sha256_init(x) ssh2_ossl_sha256_init(x) +#define ssh2_sha256_update(ctx, data, len) \ + ssh2_ossl_sha256_update(&(ctx), data, len) +#define ssh2_sha256_final(ctx, out) ssh2_ossl_sha256_final(&(ctx), out) +#define ssh2_sha256(x, y, z) ssh2_ossl_sha256(x, y, z) #ifdef HAVE_OPAQUE_STRUCTS -#define libssh2_sha384_ctx EVP_MD_CTX * +#define ssh2_sha384_ctx EVP_MD_CTX * #else -#define libssh2_sha384_ctx EVP_MD_CTX +#define ssh2_sha384_ctx EVP_MD_CTX #endif /* returns 0 in case of failure */ -int _libssh2_sha384_init(libssh2_sha384_ctx *ctx); -int _libssh2_sha384_update(libssh2_sha384_ctx *ctx, - const void *data, size_t len); -int _libssh2_sha384_final(libssh2_sha384_ctx *ctx, unsigned char *out); -int _libssh2_sha384(const unsigned char *message, size_t len, - unsigned char *out); -#define libssh2_sha384_init(x) _libssh2_sha384_init(x) -#define libssh2_sha384_update(ctx, data, len) \ - _libssh2_sha384_update(&(ctx), data, len) -#define libssh2_sha384_final(ctx, out) _libssh2_sha384_final(&(ctx), out) -#define libssh2_sha384(x, y, z) _libssh2_sha384(x, y, z) +int ssh2_ossl_sha384_init(ssh2_sha384_ctx *ctx); +int ssh2_ossl_sha384_update(ssh2_sha384_ctx *ctx, const void *data, + size_t len); +int ssh2_ossl_sha384_final(ssh2_sha384_ctx *ctx, unsigned char *out); +int ssh2_ossl_sha384(const unsigned char *message, size_t len, + unsigned char *out); +#define ssh2_sha384_init(x) ssh2_ossl_sha384_init(x) +#define ssh2_sha384_update(ctx, data, len) \ + ssh2_ossl_sha384_update(&(ctx), data, len) +#define ssh2_sha384_final(ctx, out) ssh2_ossl_sha384_final(&(ctx), out) +#define ssh2_sha384(x, y, z) ssh2_ossl_sha384(x, y, z) #ifdef HAVE_OPAQUE_STRUCTS -#define libssh2_sha512_ctx EVP_MD_CTX * +#define ssh2_sha512_ctx EVP_MD_CTX * #else -#define libssh2_sha512_ctx EVP_MD_CTX +#define ssh2_sha512_ctx EVP_MD_CTX #endif /* returns 0 in case of failure */ -int _libssh2_sha512_init(libssh2_sha512_ctx *ctx); -int _libssh2_sha512_update(libssh2_sha512_ctx *ctx, - const void *data, size_t len); -int _libssh2_sha512_final(libssh2_sha512_ctx *ctx, unsigned char *out); -int _libssh2_sha512(const unsigned char *message, size_t len, - unsigned char *out); -#define libssh2_sha512_init(x) _libssh2_sha512_init(x) -#define libssh2_sha512_update(ctx, data, len) \ - _libssh2_sha512_update(&(ctx), data, len) -#define libssh2_sha512_final(ctx, out) _libssh2_sha512_final(&(ctx), out) -#define libssh2_sha512(x, y, z) _libssh2_sha512(x, y, z) +int ssh2_ossl_sha512_init(ssh2_sha512_ctx *ctx); +int ssh2_ossl_sha512_update(ssh2_sha512_ctx *ctx, const void *data, + size_t len); +int ssh2_ossl_sha512_final(ssh2_sha512_ctx *ctx, unsigned char *out); +int ssh2_ossl_sha512(const unsigned char *message, size_t len, + unsigned char *out); +#define ssh2_sha512_init(x) ssh2_ossl_sha512_init(x) +#define ssh2_sha512_update(ctx, data, len) \ + ssh2_ossl_sha512_update(&(ctx), data, len) +#define ssh2_sha512_final(ctx, out) ssh2_ossl_sha512_final(&(ctx), out) +#define ssh2_sha512(x, y, z) ssh2_ossl_sha512(x, y, z) #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM #ifdef HAVE_OPAQUE_STRUCTS -#define libssh2_md5_ctx EVP_MD_CTX * +#define ssh2_md5_ctx EVP_MD_CTX * #else -#define libssh2_md5_ctx EVP_MD_CTX +#define ssh2_md5_ctx EVP_MD_CTX #endif /* returns 0 in case of failure */ -int _libssh2_md5_init(libssh2_md5_ctx *ctx); -int _libssh2_md5_update(libssh2_md5_ctx *ctx, - const void *data, size_t len); -int _libssh2_md5_final(libssh2_md5_ctx *ctx, unsigned char *out); -#define libssh2_md5_init(x) _libssh2_md5_init(x) -#define libssh2_md5_update(ctx, data, len) \ - _libssh2_md5_update(&(ctx), data, len) -#define libssh2_md5_final(ctx, out) _libssh2_md5_final(&(ctx), out) +int ssh2_ossl_md5_init(ssh2_md5_ctx *ctx); +int ssh2_ossl_md5_update(ssh2_md5_ctx *ctx, const void *data, size_t len); +int ssh2_ossl_md5_final(ssh2_md5_ctx *ctx, unsigned char *out); +#define ssh2_md5_init(x) ssh2_ossl_md5_init(x) +#define ssh2_md5_update(ctx, data, len) ssh2_ossl_md5_update(&(ctx), data, len) +#define ssh2_md5_final(ctx, out) ssh2_ossl_md5_final(&(ctx), out) #endif /* LIBSSH2_MD5 || LIBSSH2_MD5_PEM */ #ifdef USE_OPENSSL_3 -#define libssh2_hmac_ctx EVP_MAC_CTX * +#define ssh2_hmac_ctx EVP_MAC_CTX * #elif defined(HAVE_OPAQUE_STRUCTS) -#define libssh2_hmac_ctx HMAC_CTX * +#define ssh2_hmac_ctx HMAC_CTX * #else /* !HAVE_OPAQUE_STRUCTS */ -#define libssh2_hmac_ctx HMAC_CTX +#define ssh2_hmac_ctx HMAC_CTX #endif /* USE_OPENSSL_3 */ -void _libssh2_openssl_crypto_init(void); -void _libssh2_openssl_crypto_exit(void); -#define libssh2_crypto_init() _libssh2_openssl_crypto_init() -#define libssh2_crypto_exit() _libssh2_openssl_crypto_exit() +void ssh2_ossl_init(void); +void ssh2_ossl_exit(void); +#define ssh2_crypto_init() ssh2_ossl_init() +#define ssh2_crypto_exit() ssh2_ossl_exit() #if LIBSSH2_RSA #ifdef USE_OPENSSL_3 -#define libssh2_rsa_ctx EVP_PKEY -#define _libssh2_rsa_free(rsactx) EVP_PKEY_free(rsactx) +#define ssh2_rsa_ctx EVP_PKEY +#define ssh2_rsa_free(rsactx) EVP_PKEY_free(rsactx) #else -#define libssh2_rsa_ctx RSA -#define _libssh2_rsa_free(rsactx) RSA_free(rsactx) +#define ssh2_rsa_ctx RSA +#define ssh2_rsa_free(rsactx) RSA_free(rsactx) #endif #endif /* LIBSSH2_RSA */ @@ -351,11 +348,11 @@ void _libssh2_openssl_crypto_exit(void); #if LIBSSH2_DSA #ifdef USE_OPENSSL_3 -#define libssh2_dsa_ctx EVP_PKEY -#define _libssh2_dsa_free(rsactx) EVP_PKEY_free(rsactx) +#define ssh2_dsa_ctx EVP_PKEY +#define ssh2_dsa_free(rsactx) EVP_PKEY_free(rsactx) #else -#define libssh2_dsa_ctx DSA -#define _libssh2_dsa_free(dsactx) DSA_free(dsactx) +#define ssh2_dsa_ctx DSA +#define ssh2_dsa_free(dsactx) DSA_free(dsactx) #endif #endif /* LIBSSH2_DSA */ @@ -363,100 +360,99 @@ void _libssh2_openssl_crypto_exit(void); #if LIBSSH2_ECDSA #ifdef USE_OPENSSL_3 -#define libssh2_ecdsa_ctx EVP_PKEY -#define _libssh2_ecdsa_free(ecdsactx) EVP_PKEY_free(ecdsactx) -#define libssh2_ec_key EVP_PKEY +#define ssh2_ecdsa_ctx EVP_PKEY +#define ssh2_ecdsa_free(ecdsactx) EVP_PKEY_free(ecdsactx) +#define ssh2_ec_key EVP_PKEY #else -#define libssh2_ecdsa_ctx EC_KEY -#define _libssh2_ecdsa_free(ecdsactx) EC_KEY_free(ecdsactx) -#define libssh2_ec_key EC_KEY +#define ssh2_ecdsa_ctx EC_KEY +#define ssh2_ecdsa_free(ecdsactx) EC_KEY_free(ecdsactx) +#define ssh2_ec_key EC_KEY #endif typedef enum { - LIBSSH2_EC_CURVE_NISTP256 = NID_X9_62_prime256v1, - LIBSSH2_EC_CURVE_NISTP384 = NID_secp384r1, - LIBSSH2_EC_CURVE_NISTP521 = NID_secp521r1 -} libssh2_curve_type; + SSH2_EC_CURVE_NISTP256 = NID_X9_62_prime256v1, + SSH2_EC_CURVE_NISTP384 = NID_secp384r1, + SSH2_EC_CURVE_NISTP521 = NID_secp521r1 +} ssh2_curve_type; #else /* !LIBSSH2_ECDSA */ -#define libssh2_ec_key void +#define ssh2_ec_key void #endif /* LIBSSH2_ECDSA */ #if LIBSSH2_ED25519 -#define libssh2_ed25519_ctx EVP_PKEY -#define _libssh2_ed25519_free(ctx) EVP_PKEY_free(ctx) +#define ssh2_ed25519_ctx EVP_PKEY +#define ssh2_ed25519_free(ctx) EVP_PKEY_free(ctx) #endif /* LIBSSH2_ED25519 */ -#define LIBSSH2_CIPHER_T(name) const EVP_CIPHER *(*(name))(void) +#define SSH2_CIPHER_T(name) const EVP_CIPHER *(*(name))(void) #ifdef HAVE_OPAQUE_STRUCTS -#define libssh2_cipher_ctx EVP_CIPHER_CTX * +#define ssh2_cipher_ctx EVP_CIPHER_CTX * #else -#define libssh2_cipher_ctx EVP_CIPHER_CTX +#define ssh2_cipher_ctx EVP_CIPHER_CTX #endif -#define libssh2_cipher_aes256gcm EVP_aes_256_gcm -#define libssh2_cipher_aes128gcm EVP_aes_128_gcm +#define ssh2_cipher_aes256gcm EVP_aes_256_gcm +#define ssh2_cipher_aes128gcm EVP_aes_128_gcm -#define libssh2_cipher_aes256 EVP_aes_256_cbc -#define libssh2_cipher_aes192 EVP_aes_192_cbc -#define libssh2_cipher_aes128 EVP_aes_128_cbc -#define libssh2_cipher_aes128ctr EVP_aes_128_ctr -#define libssh2_cipher_aes192ctr EVP_aes_192_ctr -#define libssh2_cipher_aes256ctr EVP_aes_256_ctr -#define libssh2_cipher_blowfish EVP_bf_cbc -#define libssh2_cipher_arcfour EVP_rc4 -#define libssh2_cipher_cast5 EVP_cast5_cbc -#define libssh2_cipher_3des EVP_des_ede3_cbc -#define libssh2_cipher_chacha20 NULL +#define ssh2_cipher_aes256 EVP_aes_256_cbc +#define ssh2_cipher_aes192 EVP_aes_192_cbc +#define ssh2_cipher_aes128 EVP_aes_128_cbc +#define ssh2_cipher_aes128ctr EVP_aes_128_ctr +#define ssh2_cipher_aes192ctr EVP_aes_192_ctr +#define ssh2_cipher_aes256ctr EVP_aes_256_ctr +#define ssh2_cipher_blowfish EVP_bf_cbc +#define ssh2_cipher_arcfour EVP_rc4 +#define ssh2_cipher_cast5 EVP_cast5_cbc +#define ssh2_cipher_3des EVP_des_ede3_cbc +#define ssh2_cipher_chacha20 NULL #ifdef HAVE_OPAQUE_STRUCTS -#define _libssh2_cipher_dtor(ctx) EVP_CIPHER_CTX_free(*(ctx)) +#define ssh2_cipher_dtor(ctx) EVP_CIPHER_CTX_free(*(ctx)) #else -#define _libssh2_cipher_dtor(ctx) EVP_CIPHER_CTX_cleanup(ctx) +#define ssh2_cipher_dtor(ctx) EVP_CIPHER_CTX_cleanup(ctx) #endif -#define libssh2_bn BIGNUM -#define libssh2_bn_ctx BN_CTX -#define _libssh2_bn_ctx_new() BN_CTX_new() -#define _libssh2_bn_ctx_free(bnctx) BN_CTX_free(bnctx) -#define _libssh2_bn_init() BN_new() -#define _libssh2_bn_init_from_bin() _libssh2_bn_init() -#define _libssh2_bn_set_word(bn, val) !BN_set_word(bn, val) -int _libssh2_bn_from_bin(libssh2_bn *bn, size_t len, - const unsigned char *val); -#define _libssh2_bn_to_bin(bn, val) (BN_bn2bin(bn, val) <= 0) -#define _libssh2_bn_bytes(bn) BN_num_bytes(bn) -#define _libssh2_bn_bits(bn) BN_num_bits(bn) -#define _libssh2_bn_free(bn) BN_clear_free(bn) +#define ssh2_bn BIGNUM +#define ssh2_bn_ctx BN_CTX +#define ssh2_bn_ctx_new() BN_CTX_new() +#define ssh2_bn_ctx_free(bnctx) BN_CTX_free(bnctx) +#define ssh2_bn_init() BN_new() +#define ssh2_bn_init_from_bin() ssh2_bn_init() +#define ssh2_bn_set_word(bn, val) !BN_set_word(bn, val) +int ssh2_bn_from_bin(ssh2_bn *bn, size_t len, const unsigned char *val); +#define ssh2_bn_to_bin(bn, val) (BN_bn2bin(bn, val) <= 0) +#define ssh2_bn_bytes(bn) BN_num_bytes(bn) +#define ssh2_bn_bits(bn) BN_num_bits(bn) +#define ssh2_bn_free(bn) BN_clear_free(bn) /* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */ -#define LIBSSH2_DH_GEX_MINGROUP 2048 -#define LIBSSH2_DH_GEX_OPTGROUP 4096 -#define LIBSSH2_DH_GEX_MAXGROUP 8192 +#define SSH2_DH_GEX_MINGROUP 2048 +#define SSH2_DH_GEX_OPTGROUP 4096 +#define SSH2_DH_GEX_MAXGROUP 8192 -#define LIBSSH2_DH_MAX_MODULUS_BITS 16384 +#define SSH2_DH_MAX_MODULUS_BITS 16384 -#define libssh2_dh_ctx BIGNUM * -#define libssh2_dh_init(dhctx) _libssh2_dh_init(dhctx) -#define libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ - _libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) -#define libssh2_dh_secret(dhctx, secret, f, p, bnctx) \ - _libssh2_dh_secret(dhctx, secret, f, p, bnctx) -#define libssh2_dh_dtor(dhctx) _libssh2_dh_dtor(dhctx) -void _libssh2_dh_init(libssh2_dh_ctx *dhctx); -int _libssh2_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, - int group_order, - libssh2_bn_ctx *bnctx); -int _libssh2_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p, - libssh2_bn_ctx *bnctx); -void _libssh2_dh_dtor(libssh2_dh_ctx *dhctx); +#define ssh2_dh_ctx BIGNUM * +#define ssh2_dh_init(dhctx) ssh2_ossl_dh_init(dhctx) +#define ssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ + ssh2_ossl_dh_key_pair(dhctx, public, g, p, group_order, bnctx) +#define ssh2_dh_secret(dhctx, secret, f, p, bnctx) \ + ssh2_ossl_dh_secret(dhctx, secret, f, p, bnctx) +#define ssh2_dh_dtor(dhctx) ssh2_ossl_dh_dtor(dhctx) +void ssh2_ossl_dh_init(ssh2_dh_ctx *dhctx); +int ssh2_ossl_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_ossl_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p, + ssh2_bn_ctx *bnctx); +void ssh2_ossl_dh_dtor(ssh2_dh_ctx *dhctx); -int _libssh2_openssl_random(void *buf, size_t len); +int ssh2_ossl_random(void *buf, size_t len); -const EVP_CIPHER *_libssh2_EVP_aes_128_ctr(void); -const EVP_CIPHER *_libssh2_EVP_aes_192_ctr(void); -const EVP_CIPHER *_libssh2_EVP_aes_256_ctr(void); +const EVP_CIPHER *ssh2_EVP_aes_128_ctr(void); +const EVP_CIPHER *ssh2_EVP_aes_192_ctr(void); +const EVP_CIPHER *ssh2_EVP_aes_256_ctr(void); #endif /* LIBSSH2_OPENSSL_H */ diff --git a/src/os400qc3.c b/src/os400qc3.c index 1fc915ee..547134b5 100644 --- a/src/os400qc3.c +++ b/src/os400qc3.c @@ -194,8 +194,8 @@ static const struct pkcs5algo hmacWithSHA1 = { static const unsigned char OID_desCBC[] = { 5, 40 + 3, 0x0E, 0x03, 0x02, 0x07 }; -static int parse_iv(LIBSSH2_SESSION *session, struct pkcs5params *pkcs5, - struct pkcs5algo *algo, struct asn1Element *param); +static int parse_iv(LIBSSH2_SESSION *session, struct pkcs5params *pkcs5, + struct pkcs5algo *algo, struct asn1Element *param); static const struct pkcs5algo desCBC = { OID_desCBC, parse_iv, Qc3_DES, 8, Qc3_CBC, Qc3_Pad_Counter, '\0', 8, 0, 0, 8, 8, 0 @@ -358,7 +358,7 @@ static struct asn1Element lastbytebitcount = { * *******************************************************************/ -int _libssh2_random(unsigned char *buf, size_t len) +int ssh2_random(unsigned char *buf, size_t len) { Qus_EC_t errcode; @@ -368,11 +368,11 @@ int _libssh2_random(unsigned char *buf, size_t len) return errcode.Bytes_Available ? -1 : 0; } -libssh2_bn *_libssh2_bn_init(void) +ssh2_bn *ssh2_bn_init(void) { - libssh2_bn *bignum; + ssh2_bn *bignum; - bignum = (libssh2_bn *)malloc(sizeof(*bignum)); + bignum = (ssh2_bn *)malloc(sizeof(*bignum)); if(bignum) { bignum->bignum = NULL; bignum->length = 0; @@ -381,12 +381,12 @@ libssh2_bn *_libssh2_bn_init(void) return bignum; } -void _libssh2_bn_free(libssh2_bn *bn) +void ssh2_bn_free(ssh2_bn *bn) { if(bn) { if(bn->bignum) { if(bn->length) - _libssh2_explicit_zero(bn->bignum, bn->length); + ssh2_explicit_zero(bn->bignum, bn->length); free(bn->bignum); } @@ -395,7 +395,7 @@ void _libssh2_bn_free(libssh2_bn *bn) } } -static int _libssh2_bn_resize(libssh2_bn *bn, size_t newlen) +static int bn_resize(ssh2_bn *bn, size_t newlen) { unsigned char *bignum; @@ -408,7 +408,7 @@ static int _libssh2_bn_resize(libssh2_bn *bn, size_t newlen) bignum = (unsigned char *)malloc(newlen); else { if(newlen < bn->length) - _libssh2_explicit_zero(bn->bignum + newlen, bn->length - newlen); + ssh2_explicit_zero(bn->bignum + newlen, bn->length - newlen); if(!newlen) { free((char *)bn->bignum); @@ -430,7 +430,7 @@ static int _libssh2_bn_resize(libssh2_bn *bn, size_t newlen) return 0; } -unsigned long _libssh2_bn_bits(libssh2_bn *bn) +unsigned long ssh2_bn_bits(ssh2_bn *bn) { unsigned int i; unsigned char b; @@ -451,7 +451,7 @@ unsigned long _libssh2_bn_bits(libssh2_bn *bn) return 0; } -int _libssh2_bn_from_bin(libssh2_bn *bn, size_t len, const unsigned char *val) +int ssh2_bn_from_bin(ssh2_bn *bn, size_t len, const unsigned char *val) { size_t i; @@ -461,7 +461,7 @@ int _libssh2_bn_from_bin(libssh2_bn *bn, size_t len, const unsigned char *val) for(; len && !*val; len--) val++; - if(_libssh2_bn_resize(bn, len)) + if(bn_resize(bn, len)) return -1; for(i = len; i--;) @@ -470,13 +470,13 @@ int _libssh2_bn_from_bin(libssh2_bn *bn, size_t len, const unsigned char *val) return 0; } -int _libssh2_bn_set_word(libssh2_bn *bn, unsigned long val) +int ssh2_bn_set_word(ssh2_bn *bn, unsigned long val) { val = htonl(val); - return _libssh2_bn_from_bin(bn, sizeof(val), (unsigned char *)&val); + return ssh2_bn_from_bin(bn, sizeof(val), (unsigned char *)&val); } -int _libssh2_bn_to_bin(libssh2_bn *bn, unsigned char *val) +int ssh2_bn_to_bin(ssh2_bn *bn, unsigned char *val) { int i; @@ -489,22 +489,6 @@ int _libssh2_bn_to_bin(libssh2_bn *bn, unsigned char *val) return 0; } -static int _libssh2_bn_from_bn(libssh2_bn *to, libssh2_bn *from) -{ - int i; - - if(!to || !from) - return -1; - - if(_libssh2_bn_resize(to, from->length)) - return -1; - - for(i = to->length; i--;) - to->bignum[i] = from->bignum[i]; - - return 0; -} - /******************************************************************* * * OS/400 QC3 crypto-library backend: ASN.1 support. @@ -646,7 +630,7 @@ static void asn1delete(struct asn1Element *e) } } -static struct asn1Element *asn1uint(libssh2_bn *bn) +static struct asn1Element *asn1uint(ssh2_bn *bn) { struct asn1Element *e; int bits; @@ -656,7 +640,7 @@ static struct asn1Element *asn1uint(libssh2_bn *bn) if(!bn) return NULL; - bits = _libssh2_bn_bits(bn); + bits = ssh2_bn_bits(bn); length = (bits + 8) >> 3; e = asn1_new(ASN1_INTEGER, length); @@ -664,7 +648,7 @@ static struct asn1Element *asn1uint(libssh2_bn *bn) p = e->beg; if(!(bits & 0x07)) *p++ = 0; - _libssh2_bn_to_bin(bn, p); + ssh2_bn_to_bin(bn, p); } return e; @@ -718,7 +702,7 @@ static struct asn1Element *asn1bytes(unsigned int type, return e; } -static struct asn1Element *rsapublickey(libssh2_bn *e, libssh2_bn *m) +static struct asn1Element *rsapublickey(ssh2_bn *e, ssh2_bn *m) { struct asn1Element *publicexponent; struct asn1Element *modulus; @@ -741,14 +725,14 @@ static struct asn1Element *rsapublickey(libssh2_bn *e, libssh2_bn *m) return rsapubkey; } -static struct asn1Element *rsaprivatekey(libssh2_bn *e, - libssh2_bn *m, - libssh2_bn *d, - libssh2_bn *p, - libssh2_bn *q, - libssh2_bn *exp1, - libssh2_bn *exp2, - libssh2_bn *coeff) +static struct asn1Element *rsaprivatekey(ssh2_bn *e, + ssh2_bn *m, + ssh2_bn *d, + ssh2_bn *p, + ssh2_bn *q, + ssh2_bn *exp1, + ssh2_bn *exp2, + ssh2_bn *coeff) { struct asn1Element *version; struct asn1Element *modulus; @@ -906,7 +890,7 @@ static int null_token(const char *token) sizeof(nulltoken.Key_Context_Token)); } -void _libssh2_os400qc3_crypto_dtor(struct os400qc3_crypto_ctx *x) +void ssh2_os400qc3_crypto_dtor(struct os400qc3_crypto_ctx *x) { if(!x) return; @@ -920,7 +904,7 @@ void _libssh2_os400qc3_crypto_dtor(struct os400qc3_crypto_ctx *x) memset(x->key.Key_Context_Token, 0, sizeof(x->key.Key_Context_Token)); } if(x->kek) { - _libssh2_os400qc3_crypto_dtor(x->kek); + ssh2_os400qc3_crypto_dtor(x->kek); free((char *)x->kek); x->kek = NULL; } @@ -932,8 +916,7 @@ void _libssh2_os400qc3_crypto_dtor(struct os400qc3_crypto_ctx *x) * *******************************************************************/ -int _libssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, - unsigned int algorithm) +int ssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, unsigned int algorithm) { Qc3_Format_ALGD0500_T algd; Qus_EC_t errcode; @@ -950,8 +933,8 @@ int _libssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, return errcode.Bytes_Available ? 0 : 1; } -int _libssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx, - const unsigned char *data, int len) +int ssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx, + const unsigned char *data, int len) { char dummy[64]; Qus_EC_t errcode; @@ -963,8 +946,7 @@ int _libssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx, return errcode.Bytes_Available ? 0 : 1; } -int _libssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, - unsigned char *out) +int ssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, unsigned char *out) { char data; Qus_EC_t errcode; @@ -978,14 +960,14 @@ int _libssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, return errcode.Bytes_Available ? 0 : 1; } -int _libssh2_os400qc3_hash(const unsigned char *message, unsigned long len, - unsigned char *out, unsigned int algo) +int ssh2_os400qc3_hash(const unsigned char *message, unsigned long len, + unsigned char *out, unsigned int algo) { Qc3_Format_ALGD0100_T ctx; - if(!_libssh2_os400qc3_hash_init(&ctx, algo) || - !_libssh2_os400qc3_hash_update(&ctx, message, len) || - !_libssh2_os400qc3_hash_final(&ctx, out)) + if(!ssh2_os400qc3_hash_init(&ctx, algo) || + !ssh2_os400qc3_hash_update(&ctx, message, len) || + !ssh2_os400qc3_hash_final(&ctx, out)) return 1; return 0; @@ -1008,7 +990,7 @@ static int os400qc3_hmac_init(struct os400qc3_crypto_ctx *ctx, key = (void *)lkey; keylen = minkeylen; } - if(!_libssh2_os400qc3_hash_init(&ctx->hash, algo)) + if(!ssh2_os400qc3_hash_init(&ctx->hash, algo)) return 0; set_EC_length(errcode, sizeof(errcode)); Qc3CreateKeyContext((char *)key, &keylen, binstring, &algo, qc3clear, @@ -1017,41 +999,40 @@ static int os400qc3_hmac_init(struct os400qc3_crypto_ctx *ctx, return errcode.Bytes_Available ? 0 : 1; } -int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx) +int ssh2_hmac_ctx_init(ssh2_hmac_ctx *ctx) { - memset((char *)ctx, 0, sizeof(libssh2_hmac_ctx)); + memset((char *)ctx, 0, sizeof(ssh2_hmac_ctx)); return 1; } #if LIBSSH2_MD5 -int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) - void *key, size_t keylen) +int ssh2_hmac_md5_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) + void *key, size_t keylen) { return os400qc3_hmac_init(ctx, Qc3_MD5, MD5_DIGEST_LENGTH, key, keylen); } #endif -int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha1_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { return os400qc3_hmac_init(ctx, Qc3_SHA1, SHA_DIGEST_LENGTH, key, keylen); } -int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha256_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { return os400qc3_hmac_init(ctx, Qc3_SHA256, SHA256_DIGEST_LENGTH, key, keylen); } -int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, void *key, size_t keylen) +int ssh2_hmac_sha512_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { return os400qc3_hmac_init(ctx, Qc3_SHA512, SHA512_DIGEST_LENGTH, key, keylen); } -int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, - const void *data, size_t datalen) +int ssh2_hmac_update(ssh2_hmac_ctx *ctx, const void *data, size_t datalen) { char dummy[64]; int len = (int)datalen; @@ -1065,7 +1046,7 @@ int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, return errcode.Bytes_Available ? 0 : 1; } -int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *out) +int ssh2_hmac_final(ssh2_hmac_ctx *ctx, void *out) { char data; Qus_EC_t errcode; @@ -1078,9 +1059,9 @@ int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *out) return errcode.Bytes_Available ? 0 : 1; } -void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) +void ssh2_hmac_cleanup(ssh2_hmac_ctx *ctx) { - _libssh2_os400qc3_crypto_dtor(ctx); + ssh2_os400qc3_crypto_dtor(ctx); } /******************************************************************* @@ -1089,8 +1070,8 @@ void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) * *******************************************************************/ -int _libssh2_cipher_init(libssh2_cipher_ctx *h, LIBSSH2_CIPHER_T(algo), - unsigned char *iv, unsigned char *secret, int encrypt) +int ssh2_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo), + unsigned char *iv, unsigned char *secret, int encrypt) { Qc3_Format_ALGD0200_T algd; Qus_EC_t errcode; @@ -1121,17 +1102,16 @@ int _libssh2_cipher_init(libssh2_cipher_ctx *h, LIBSSH2_CIPHER_T(algo), &algo.algo, qc3clear, NULL, NULL, h->key.Key_Context_Token, (char *)&errcode); if(errcode.Bytes_Available) { - _libssh2_os400qc3_crypto_dtor(h); + ssh2_os400qc3_crypto_dtor(h); return -1; } return 0; } -int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, - LIBSSH2_CIPHER_T(algo), - int encrypt, unsigned char *block, size_t blocksize, - int firstlast) +int ssh2_cipher_crypt(ssh2_cipher_ctx *ctx, SSH2_CIPHER_T(algo), + int encrypt, unsigned char *block, size_t blocksize, + int firstlast) { Qus_EC_t errcode; int outlen; @@ -1160,25 +1140,25 @@ int _libssh2_cipher_crypt(libssh2_cipher_ctx *ctx, * *******************************************************************/ -int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, - const unsigned char *edata, unsigned long elen, - const unsigned char *ndata, unsigned long nlen, - const unsigned char *ddata, unsigned long dlen, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *e1data, unsigned long e1len, - const unsigned char *e2data, unsigned long e2len, - const unsigned char *coeffdata, unsigned long coefflen) +int ssh2_rsa_new(ssh2_rsa_ctx **rsa, + const unsigned char *edata, unsigned long elen, + const unsigned char *ndata, unsigned long nlen, + const unsigned char *ddata, unsigned long dlen, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *e1data, unsigned long e1len, + const unsigned char *e2data, unsigned long e2len, + const unsigned char *coeffdata, unsigned long coefflen) { - libssh2_rsa_ctx *ctx; - libssh2_bn *e = _libssh2_bn_init_from_bin(); - libssh2_bn *n = _libssh2_bn_init_from_bin(); - libssh2_bn *d = NULL; - libssh2_bn *p = NULL; - libssh2_bn *q = NULL; - libssh2_bn *e1 = NULL; - libssh2_bn *e2 = NULL; - libssh2_bn *coeff = NULL; + ssh2_rsa_ctx *ctx; + ssh2_bn *e = ssh2_bn_init_from_bin(); + ssh2_bn *n = ssh2_bn_init_from_bin(); + ssh2_bn *d = NULL; + ssh2_bn *p = NULL; + ssh2_bn *q = NULL; + ssh2_bn *e1 = NULL; + ssh2_bn *e2 = NULL; + ssh2_bn *coeff = NULL; struct asn1Element *key = NULL; struct asn1Element *structkey = NULL; int keytype; @@ -1189,25 +1169,25 @@ int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, if(!ctx) ret = -1; if(!ret) { - _libssh2_bn_from_bin(e, elen, edata); - _libssh2_bn_from_bin(n, nlen, ndata); + ssh2_bn_from_bin(e, elen, edata); + ssh2_bn_from_bin(n, nlen, ndata); if(!e || !n) ret = -1; } if(!ret && ddata) { /* Private key. */ - d = _libssh2_bn_init_from_bin(); - _libssh2_bn_from_bin(d, dlen, ddata); - p = _libssh2_bn_init_from_bin(); - _libssh2_bn_from_bin(p, plen, pdata); - q = _libssh2_bn_init_from_bin(); - _libssh2_bn_from_bin(q, qlen, qdata); - e1 = _libssh2_bn_init_from_bin(); - _libssh2_bn_from_bin(e1, e1len, e1data); - e2 = _libssh2_bn_init_from_bin(); - _libssh2_bn_from_bin(e2, e2len, e2data); - coeff = _libssh2_bn_init_from_bin(); - _libssh2_bn_from_bin(coeff, coefflen, coeffdata); + d = ssh2_bn_init_from_bin(); + ssh2_bn_from_bin(d, dlen, ddata); + p = ssh2_bn_init_from_bin(); + ssh2_bn_from_bin(p, plen, pdata); + q = ssh2_bn_init_from_bin(); + ssh2_bn_from_bin(q, qlen, qdata); + e1 = ssh2_bn_init_from_bin(); + ssh2_bn_from_bin(e1, e1len, e1data); + e2 = ssh2_bn_init_from_bin(); + ssh2_bn_from_bin(e2, e2len, e2data); + coeff = ssh2_bn_init_from_bin(); + ssh2_bn_from_bin(coeff, coefflen, coeffdata); if(!d || !p || !q || !e1 || !e2 || !coeff) ret = -1; @@ -1239,18 +1219,18 @@ int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, ret = -1; } - _libssh2_bn_free(e); - _libssh2_bn_free(n); - _libssh2_bn_free(d); - _libssh2_bn_free(p); - _libssh2_bn_free(q); - _libssh2_bn_free(e1); - _libssh2_bn_free(e2); - _libssh2_bn_free(coeff); + ssh2_bn_free(e); + ssh2_bn_free(n); + ssh2_bn_free(d); + ssh2_bn_free(p); + ssh2_bn_free(q); + ssh2_bn_free(e1); + ssh2_bn_free(e2); + ssh2_bn_free(coeff); asn1delete(key); asn1delete(structkey); if(ret && ctx) { - _libssh2_rsa_free(ctx); + ssh2_rsa_free(ctx); ctx = NULL; } *rsa = ctx; @@ -1263,14 +1243,13 @@ int _libssh2_rsa_new(libssh2_rsa_ctx **rsa, * *******************************************************************/ -void _libssh2_os400qc3_dh_init(libssh2_dh_ctx *dhctx) +void ssh2_os400qc3_dh_init(ssh2_dh_ctx *dhctx) { memset((char *)dhctx, 0, sizeof(*dhctx)); } -int _libssh2_os400qc3_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, - int group_order) +int ssh2_os400qc3_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, int group_order) { struct asn1Element *prime; struct asn1Element *base; @@ -1305,7 +1284,7 @@ int _libssh2_os400qc3_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, return -1; } pkcs3len = pkcs3->end - pkcs3->header; - pubkeysize = (_libssh2_bn_bits(p) + 7) >> 3; + pubkeysize = (ssh2_bn_bits(p) + 7) >> 3; pubkey = alloca(pubkeysize); set_EC_length(errcode, sizeof(errcode)); Qc3GenDHKeyPair((char *)pkcs3->header, &pkcs3len, anycsp, NULL, @@ -1313,11 +1292,11 @@ int _libssh2_os400qc3_dh_key_pair(libssh2_dh_ctx *dhctx, libssh2_bn *public, asn1delete(pkcs3); if(errcode.Bytes_Available) return -1; - return _libssh2_bn_from_bin(public, pubkeylen, (unsigned char *)pubkey); + return ssh2_bn_from_bin(public, pubkeylen, (unsigned char *)pubkey); } -int _libssh2_os400qc3_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p) +int ssh2_os400qc3_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p) { char *pubkey; int pubkeysize; @@ -1326,21 +1305,20 @@ int _libssh2_os400qc3_dh_secret(libssh2_dh_ctx *dhctx, libssh2_bn *secret, int secretbuflen; Qus_EC_t errcode; - pubkeysize = (_libssh2_bn_bits(f) + 7) >> 3; + pubkeysize = (ssh2_bn_bits(f) + 7) >> 3; pubkey = alloca(pubkeysize); - _libssh2_bn_to_bin(f, pubkey); - secretbufsize = (_libssh2_bn_bits(p) + 7) >> 3; + ssh2_bn_to_bin(f, pubkey); + secretbufsize = (ssh2_bn_bits(p) + 7) >> 3; secretbuf = alloca(secretbufsize); set_EC_length(errcode, sizeof(errcode)); Qc3CalculateDHSecretKey(dhctx->token, pubkey, &pubkeysize, secretbuf, &secretbufsize, &secretbuflen, &errcode); if(errcode.Bytes_Available) return -1; - return _libssh2_bn_from_bin(secret, - secretbuflen, (unsigned char *)secretbuf); + return ssh2_bn_from_bin(secret, secretbuflen, (unsigned char *)secretbuf); } -void _libssh2_os400qc3_dh_dtor(libssh2_dh_ctx *dhctx) +void ssh2_os400qc3_dh_dtor(ssh2_dh_ctx *dhctx) { if(!null_token(dhctx->token)) { Qc3DestroyAlgorithmContext(dhctx->token, (char *)&ecnull); @@ -1383,8 +1361,7 @@ static int asn1getword(struct asn1Element *e, unsigned long *v) } static int pbkdf1(LIBSSH2_SESSION *session, char **dk, - const unsigned char *passphrase, - struct pkcs5params *pkcs5) + const unsigned char *passphrase, struct pkcs5params *pkcs5) { int i; Qc3_Format_ALGD0100_T hctx; @@ -1397,7 +1374,7 @@ static int pbkdf1(LIBSSH2_SESSION *session, char **dk, return -1; /* Allocate the derived key buffer. */ - *dk = LIBSSH2_ALLOC(session, pkcs5->hashlen); + *dk = SSH2_ALLOC(session, pkcs5->hashlen); if(!*dk) return -1; @@ -1405,9 +1382,8 @@ static int pbkdf1(LIBSSH2_SESSION *session, char **dk, errcode.Bytes_Available = 1; /* Defaults to error flagging. */ /* Initial hash. */ - if(_libssh2_os400qc3_hash_init(&hctx, pkcs5->hash)) { - if(_libssh2_os400qc3_hash_update(&hctx, - passphrase, strlen(passphrase))) { + if(ssh2_os400qc3_hash_init(&hctx, pkcs5->hash)) { + if(ssh2_os400qc3_hash_update(&hctx, passphrase, strlen(passphrase))) { hctx.Final_Op_Flag = Qc3_Final; Qc3CalculateHash((char *)pkcs5->salt, &len, Qc3_Data, (char *)&hctx, Qc3_Alg_Token, anycsp, NULL, *dk, @@ -1425,7 +1401,7 @@ static int pbkdf1(LIBSSH2_SESSION *session, char **dk, } if(errcode.Bytes_Available) { - LIBSSH2_FREE(session, *dk); + SSH2_FREE(session, *dk); *dk = NULL; return -1; } @@ -1440,8 +1416,7 @@ static int pbkdf1(LIBSSH2_SESSION *session, char **dk, } static int pbkdf2(LIBSSH2_SESSION *session, char **dk, - const unsigned char *passphrase, - struct pkcs5params *pkcs5) + const unsigned char *passphrase, struct pkcs5params *pkcs5) { size_t i; size_t k; @@ -1469,7 +1444,7 @@ static int pbkdf2(LIBSSH2_SESSION *session, char **dk, /* Allocate the derived key buffer. */ l = t; - buf = LIBSSH2_ALLOC(session, l * pkcs5->hashlen); + buf = SSH2_ALLOC(session, l * pkcs5->hashlen); if(!buf) return -1; *dk = buf; @@ -1477,21 +1452,21 @@ static int pbkdf2(LIBSSH2_SESSION *session, char **dk, /* Process each hLen-size blocks. */ for(i = 1; i <= l; i++) { ni = htonl(i); - if(!_libssh2_hmac_update(&hctx, pkcs5->salt, pkcs5->saltlen) || - !_libssh2_hmac_update(&hctx, &ni, sizeof(ni)) || - !_libssh2_hmac_final(&hctx, mac)) { - LIBSSH2_FREE(session, buf); + if(!ssh2_hmac_update(&hctx, pkcs5->salt, pkcs5->saltlen) || + !ssh2_hmac_update(&hctx, &ni, sizeof(ni)) || + !ssh2_hmac_final(&hctx, mac)) { + SSH2_FREE(session, buf); *dk = NULL; - _libssh2_os400qc3_crypto_dtor(&hctx); + ssh2_os400qc3_crypto_dtor(&hctx); return -1; } memcpy(buf, mac, pkcs5->hashlen); for(j = 1; j < pkcs5->itercount; j++) { - if(!_libssh2_hmac_update(&hctx, mac, pkcs5->hashlen) || - !_libssh2_hmac_final(&hctx, mac)) { - LIBSSH2_FREE(session, buf); + if(!ssh2_hmac_update(&hctx, mac, pkcs5->hashlen) || + !ssh2_hmac_final(&hctx, mac)) { + SSH2_FREE(session, buf); *dk = NULL; - _libssh2_os400qc3_crypto_dtor(&hctx); + ssh2_os400qc3_crypto_dtor(&hctx); return -1; } for(k = 0; k < pkcs5->hashlen; k++) @@ -1501,7 +1476,7 @@ static int pbkdf2(LIBSSH2_SESSION *session, char **dk, } /* Computation done. Release HMAC context. */ - _libssh2_os400qc3_crypto_dtor(&hctx); + ssh2_os400qc3_crypto_dtor(&hctx); return 0; } @@ -1791,7 +1766,7 @@ static int pkcs8kek(LIBSSH2_SESSION *session, struct os400qc3_crypto_ctx **ctx, /* Create the key and algorithm context tokens. */ *ctx = init_crypto_ctx(NULL); if(!*ctx) { - LIBSSH2_FREE(session, dk); + SSH2_FREE(session, dk); return -1; } init_crypto_ctx(*ctx); @@ -1799,7 +1774,7 @@ static int pkcs8kek(LIBSSH2_SESSION *session, struct os400qc3_crypto_ctx **ctx, Qc3CreateKeyContext(dk, &pkcs5.dklen, binstring, &algd.Block_Cipher_Alg, qc3clear, NULL, NULL, (*ctx)->key.Key_Context_Token, (char *)&errcode); - LIBSSH2_FREE(session, dk); + SSH2_FREE(session, dk); if(errcode.Bytes_Available) { free((char *)*ctx); *ctx = NULL; @@ -1821,7 +1796,7 @@ static int rsapkcs8privkey(LIBSSH2_SESSION *session, const unsigned char *data, unsigned int datalen, const unsigned char *passphrase, void *loadkeydata) { - libssh2_rsa_ctx *ctx = (libssh2_rsa_ctx *)loadkeydata; + ssh2_rsa_ctx *ctx = (ssh2_rsa_ctx *)loadkeydata; char keyform = Qc3_Clear; char *kek = NULL; char *kea = NULL; @@ -1848,7 +1823,7 @@ static int rsapkcs8privkey(LIBSSH2_SESSION *session, ctx->key.Key_Context_Token, (char *)&errcode); if(errcode.Bytes_Available) { if(kekctx) - _libssh2_os400qc3_crypto_dtor(kekctx); + ssh2_os400qc3_crypto_dtor(kekctx); return -1; } ctx->kek = kekctx; @@ -1857,7 +1832,7 @@ static int rsapkcs8privkey(LIBSSH2_SESSION *session, static char *storewithlength(char *p, const char *data, int length) { - _libssh2_htonu32(p, length); + ssh2_htonu32(p, length); if(length) memcpy(p + 4, data, length); return p + 4 + length; @@ -1884,7 +1859,7 @@ static int sshrsapubkey(LIBSSH2_SESSION *session, char **sshpubkey, *e.header != ASN1_INTEGER) return -1; len = 4 + methlen + 4 + (e.end - e.beg) + 4 + (m.end - m.beg); - cp = LIBSSH2_ALLOC(session, len); + cp = SSH2_ALLOC(session, len); if(!cp) return -1; *sshpubkey = cp; @@ -1935,7 +1910,7 @@ static int rsapkcs8pubkey(LIBSSH2_SESSION *session, pkilen = pki.end - pki.beg; Qc3ExtractPublicKey(pki.beg, &pkilen, berstring, &keyform, kek, kea, buf, (int *)&datalen, &len, &errcode); - _libssh2_os400qc3_crypto_dtor(kekctx); + ssh2_os400qc3_crypto_dtor(kekctx); if(errcode.Bytes_Available) return -1; /* Get the algorithm OID and key data from SubjectPublicKeyInfo. */ @@ -1988,7 +1963,7 @@ static int pkcs1topkcs8(LIBSSH2_SESSION *session, } if(!pkcs8) return -1; - data = (unsigned char *)LIBSSH2_ALLOC(session, pkcs8->end - pkcs8->header); + data = (unsigned char *)SSH2_ALLOC(session, pkcs8->end - pkcs8->header); if(!data) { asn1delete(pkcs8); return -1; @@ -2011,7 +1986,7 @@ static int rsapkcs1privkey(LIBSSH2_SESSION *session, if(pkcs1topkcs8(session, &data8, &datalen8, data, datalen)) return -1; ret = rsapkcs8privkey(session, data8, datalen8, passphrase, loadkeydata); - LIBSSH2_FREE(session, (char *)data8); + SSH2_FREE(session, (char *)data8); return ret; } @@ -2026,7 +2001,7 @@ static int rsapkcs1pubkey(LIBSSH2_SESSION *session, if(pkcs1topkcs8(session, &data8, &datalen8, data, datalen)) return -1; ret = rsapkcs8pubkey(session, data8, datalen8, passphrase, loadkeydata); - LIBSSH2_FREE(session, (char *)data8); + SSH2_FREE(session, (char *)data8); return ret; } @@ -2042,9 +2017,8 @@ static int try_pem_load(LIBSSH2_SESSION *session, FILE *fp, fseek(fp, 0L, SEEK_SET); for(;;) { - ret = _libssh2_pem_parse(session, header, trailer, - passphrase, - fp, &data, &datalen); + ret = ssh2_pem_parse(session, header, trailer, + passphrase, fp, &data, &datalen); if(!ret) { ret = (*proc)(session, data, datalen, passphrase, loadkeydata); @@ -2053,7 +2027,7 @@ static int try_pem_load(LIBSSH2_SESSION *session, FILE *fp, } if(data) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); data = NULL; } c = getc(fp); @@ -2131,11 +2105,11 @@ static int load_rsa_private_file(LIBSSH2_SESSION *session, return ret; } -int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, LIBSSH2_SESSION *session, - const char *filename, - unsigned const char *passphrase) +int ssh2_rsa_new_private(ssh2_rsa_ctx **rsa, LIBSSH2_SESSION *session, + const char *filename, + unsigned const char *passphrase) { - libssh2_rsa_ctx *ctx = init_crypto_ctx(NULL); + ssh2_rsa_ctx *ctx = init_crypto_ctx(NULL); int ret; if(!ctx) @@ -2144,18 +2118,18 @@ int _libssh2_rsa_new_private(libssh2_rsa_ctx **rsa, LIBSSH2_SESSION *session, rsapkcs1privkey, rsapkcs8privkey, (void *)ctx); if(ret) { - _libssh2_os400qc3_crypto_dtor(ctx); + ssh2_os400qc3_crypto_dtor(ctx); ctx = NULL; } *rsa = ctx; return ret; } -int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, - unsigned char **method, size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekey, const char *passphrase) +int ssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, + unsigned char **method, size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekey, const char *passphrase) { struct loadpubkeydata p; int ret; @@ -2169,7 +2143,7 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, rsapkcs1pubkey, rsapkcs8pubkey, (void *)&p); if(!ret) { *method_len = strlen(p.method); - *method = LIBSSH2_ALLOC(session, *method_len); + *method = SSH2_ALLOC(session, *method_len); if(*method) memcpy((char *)*method, p.method, *method_len); else @@ -2178,9 +2152,9 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, if(ret) { if(*method) - LIBSSH2_FREE(session, *method); + SSH2_FREE(session, *method); if(p.data) - LIBSSH2_FREE(session, (void *)p.data); + SSH2_FREE(session, (void *)p.data); *method = NULL; *method_len = 0; } @@ -2192,13 +2166,13 @@ int _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session, return ret; } -int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - unsigned const char *passphrase) +int ssh2_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + unsigned const char *passphrase) { - libssh2_rsa_ctx *ctx = init_crypto_ctx(NULL); + ssh2_rsa_ctx *ctx = init_crypto_ctx(NULL); unsigned char *data = NULL; size_t datalen = 0; int ret; @@ -2208,19 +2182,19 @@ int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, /* Try with "ENCRYPTED PRIVATE KEY" PEM armor. --> PKCS#8 EncryptedPrivateKeyInfo */ - ret = _libssh2_pem_parse_memory(session, - beginencprivkeyhdr, endencprivkeyhdr, - passphrase, - filedata, filedata_len, &data, &datalen); + ret = ssh2_pem_parse_memory(session, + beginencprivkeyhdr, endencprivkeyhdr, + passphrase, + filedata, filedata_len, &data, &datalen); /* Try with "PRIVATE KEY" PEM armor. --> PKCS#8 PrivateKeyInfo or EncryptedPrivateKeyInfo */ if(ret) - ret = _libssh2_pem_parse_memory(session, - beginprivkeyhdr, endprivkeyhdr, - passphrase, - filedata, filedata_len, - &data, &datalen); + ret = ssh2_pem_parse_memory(session, + beginprivkeyhdr, endprivkeyhdr, + passphrase, + filedata, filedata_len, + &data, &datalen); if(!ret) { /* Process PKCS#8. */ @@ -2230,11 +2204,11 @@ int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, else { /* Try with "RSA PRIVATE KEY" PEM armor. --> PKCS#1 RSAPrivateKey */ - ret = _libssh2_pem_parse_memory(session, - beginrsaprivkeyhdr, endrsaprivkeyhdr, - passphrase, - filedata, filedata_len, - &data, &datalen); + ret = ssh2_pem_parse_memory(session, + beginrsaprivkeyhdr, endrsaprivkeyhdr, + passphrase, + filedata, filedata_len, + &data, &datalen); if(!ret) ret = rsapkcs1privkey(session, data, datalen, passphrase, (void *)&ctx); @@ -2254,10 +2228,10 @@ int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, } if(data) - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); if(ret) { - _libssh2_os400qc3_crypto_dtor(ctx); + ssh2_os400qc3_crypto_dtor(ctx); ctx = NULL; } @@ -2265,13 +2239,13 @@ int _libssh2_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, return ret; } -int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { struct loadpubkeydata p; unsigned char *data = NULL; @@ -2286,34 +2260,33 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, /* Try with "ENCRYPTED PRIVATE KEY" PEM armor. --> PKCS#8 EncryptedPrivateKeyInfo */ - ret = _libssh2_pem_parse_memory(session, - beginencprivkeyhdr, endencprivkeyhdr, - passphrase, - privatekeydata, privatekeydata_len, - &data, &datalen); + ret = ssh2_pem_parse_memory(session, + beginencprivkeyhdr, endencprivkeyhdr, + passphrase, + privatekeydata, privatekeydata_len, + &data, &datalen); /* Try with "PRIVATE KEY" PEM armor. --> PKCS#8 PrivateKeyInfo or EncryptedPrivateKeyInfo */ if(ret) - ret = _libssh2_pem_parse_memory(session, - beginprivkeyhdr, endprivkeyhdr, - passphrase, - privatekeydata, privatekeydata_len, - &data, &datalen); + ret = ssh2_pem_parse_memory(session, + beginprivkeyhdr, endprivkeyhdr, + passphrase, + privatekeydata, privatekeydata_len, + &data, &datalen); if(!ret) { /* Process PKCS#8. */ - ret = rsapkcs8pubkey(session, - data, datalen, passphrase, (void *)&p); + ret = rsapkcs8pubkey(session, data, datalen, passphrase, (void *)&p); } else { /* Try with "RSA PRIVATE KEY" PEM armor. --> PKCS#1 RSAPrivateKey */ - ret = _libssh2_pem_parse_memory(session, - beginrsaprivkeyhdr, endrsaprivkeyhdr, - passphrase, - privatekeydata, privatekeydata_len, - &data, &datalen); + ret = ssh2_pem_parse_memory(session, + beginrsaprivkeyhdr, endrsaprivkeyhdr, + passphrase, + privatekeydata, privatekeydata_len, + &data, &datalen); if(!ret) ret = rsapkcs1pubkey(session, data, datalen, passphrase, (void *)&p); @@ -2333,11 +2306,11 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, } if(data) - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); if(!ret) { *method_len = strlen(p.method); - *method = LIBSSH2_ALLOC(session, *method_len); + *method = SSH2_ALLOC(session, *method_len); if(*method) memcpy((char *)*method, p.method, *method_len); else @@ -2345,9 +2318,9 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, } if(ret) { if(*method) - LIBSSH2_FREE(session, *method); + SSH2_FREE(session, *method); if(p.data) - LIBSSH2_FREE(session, (void *)p.data); + SSH2_FREE(session, (void *)p.data); *method = NULL; *method_len = 0; } @@ -2359,28 +2332,28 @@ int _libssh2_pub_priv_keyfilememory(LIBSSH2_SESSION *session, return ret; } -int _libssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - int *algorithm, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_sk_pub_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + int *algorithm, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public SK key from private key " - "file: Method unimplemented in OS/400 QC3 backend"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public SK key from private key " + "file: Method unimplemented in OS/400 QC3 backend"); } -int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, size_t hash_len, - const unsigned char *sig, size_t sig_len, - const unsigned char *m, size_t m_len) +int ssh2_rsa_sha2_verify(ssh2_rsa_ctx *rsactx, size_t hash_len, + const unsigned char *sig, size_t sig_len, + const unsigned char *m, size_t m_len) { Qus_EC_t errcode; Qc3_Format_ALGD0400_T algd; @@ -2412,21 +2385,21 @@ int _libssh2_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, size_t hash_len, return errcode.Bytes_Available ? -1 : 0; } -int _libssh2_rsa_sha1_verify(libssh2_rsa_ctx *rsactx, - const unsigned char *sig, size_t sig_len, - const unsigned char *m, size_t m_len) +int ssh2_rsa_sha1_verify(ssh2_rsa_ctx *rsactx, + const unsigned char *sig, size_t sig_len, + const unsigned char *m, size_t m_len) { - return _libssh2_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, - sig, sig_len, m, m_len); + return ssh2_rsa_sha2_verify(rsactx, SHA_DIGEST_LENGTH, + sig, sig_len, m, m_len); } -int _libssh2_os400qc3_rsa_signv(LIBSSH2_SESSION *session, - int algo, - unsigned char **signature, - size_t *signature_len, - int veccount, - const struct iovec vector[], - libssh2_rsa_ctx *ctx) +int ssh2_os400qc3_rsa_signv(LIBSSH2_SESSION *session, + int algo, + unsigned char **signature, + size_t *signature_len, + int veccount, + const struct iovec vector[], + ssh2_rsa_ctx *ctx) { Qus_EC_t errcode; Qc3_Format_ALGD0400_T algd; @@ -2447,7 +2420,7 @@ int _libssh2_os400qc3_rsa_signv(LIBSSH2_SESSION *session, (char *)&errcode); if(errcode.Bytes_Available) return -1; - sig = LIBSSH2_ALLOC(session, siglen); + sig = SSH2_ALLOC(session, siglen); if(!sig) return -1; memcpy((char *)sig, sigbuf, siglen); @@ -2459,9 +2432,9 @@ int _libssh2_os400qc3_rsa_signv(LIBSSH2_SESSION *session, /* * Return supported key hash algo upgrades, see crypto.h */ -const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session, - unsigned char *key_method, - size_t key_method_len) +const char *ssh2_supported_key_sign_algs(LIBSSH2_SESSION *session, + unsigned char *key_method, + size_t key_method_len) { (void)session; diff --git a/src/os400qc3.h b/src/os400qc3.h index 0db86c36..85bf1ab3 100644 --- a/src/os400qc3.h +++ b/src/os400qc3.h @@ -40,7 +40,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -#define LIBSSH2_CRYPTO_ENGINE libssh2_os400qc3 +#define SSH2_CRYPTO_ENGINE libssh2_os400qc3 #include #include @@ -199,7 +199,7 @@ #if LIBSSH2_ECDSA #else -#define libssh2_ec_key void +#define ssh2_ec_key void #endif /******************************************************************* @@ -220,7 +220,7 @@ struct os400qc3_bn { /* Big number. */ unsigned int length; /* Length of bignum (# bytes). */ }; -#define libssh2_bn struct os400qc3_bn +#define ssh2_bn struct os400qc3_bn struct os400qc3_cipher { /* Algorithm description. */ char *fmt; /* Format of Qc3 structure. */ @@ -240,118 +240,89 @@ struct os400qc3_dh_ctx { /* Diffie-Hellman context. */ * *******************************************************************/ -#define libssh2_crypto_init() -#define libssh2_crypto_exit() +#define ssh2_crypto_init() +#define ssh2_crypto_exit() -#define libssh2_sha1_ctx Qc3_Format_ALGD0100_T -#define libssh2_sha256_ctx Qc3_Format_ALGD0100_T -#define libssh2_sha384_ctx Qc3_Format_ALGD0100_T -#define libssh2_sha512_ctx Qc3_Format_ALGD0100_T -#define libssh2_hmac_ctx struct os400qc3_crypto_ctx -#define libssh2_cipher_ctx struct os400qc3_crypto_ctx +#define ssh2_sha1_ctx Qc3_Format_ALGD0100_T +#define ssh2_sha256_ctx Qc3_Format_ALGD0100_T +#define ssh2_sha384_ctx Qc3_Format_ALGD0100_T +#define ssh2_sha512_ctx Qc3_Format_ALGD0100_T +#define ssh2_hmac_ctx struct os400qc3_crypto_ctx +#define ssh2_cipher_ctx struct os400qc3_crypto_ctx -#define libssh2_sha1_init(x) _libssh2_os400qc3_hash_init(x, Qc3_SHA1) -#define libssh2_sha1_update(ctx, data, len) \ - _libssh2_os400qc3_hash_update(&(ctx), data, len) -#define libssh2_sha1_final(ctx, out) \ - _libssh2_os400qc3_hash_final(&(ctx), out) -#define libssh2_sha256_init(x) \ - _libssh2_os400qc3_hash_init(x, Qc3_SHA256) -#define libssh2_sha256_update(ctx, data, len) \ - _libssh2_os400qc3_hash_update(&(ctx), data, len) -#define libssh2_sha256_final(ctx, out) \ - _libssh2_os400qc3_hash_final(&(ctx), out) -#define libssh2_sha256(message, len, out) \ - _libssh2_os400qc3_hash(message, len, out, Qc3_SHA256) -#define libssh2_sha384_init(x) \ - _libssh2_os400qc3_hash_init(x, Qc3_SHA384) -#define libssh2_sha384_update(ctx, data, len) \ - _libssh2_os400qc3_hash_update(&(ctx), data, len) -#define libssh2_sha384_final(ctx, out) \ - _libssh2_os400qc3_hash_final(&(ctx), out) -#define libssh2_sha384(message, len, out) \ - _libssh2_os400qc3_hash(message, len, out, Qc3_SHA384) -#define libssh2_sha512_init(x) \ - _libssh2_os400qc3_hash_init(x, Qc3_SHA512) -#define libssh2_sha512_update(ctx, data, len) \ - _libssh2_os400qc3_hash_update(&(ctx), data, len) -#define libssh2_sha512_final(ctx, out) \ - _libssh2_os400qc3_hash_final(&(ctx), out) -#define libssh2_sha512(message, len, out) \ - _libssh2_os400qc3_hash(message, len, out, Qc3_SHA512) +#define ssh2_sha1_init(x) ssh2_os400qc3_hash_init(x, Qc3_SHA1) +#define ssh2_sha1_update(ctx, d, l) ssh2_os400qc3_hash_update(&(ctx), d, l) +#define ssh2_sha1_final(ctx, out) ssh2_os400qc3_hash_final(&(ctx), out) +#define ssh2_sha256_init(x) ssh2_os400qc3_hash_init(x, Qc3_SHA256) +#define ssh2_sha256_update(ctx, d, l) ssh2_os400qc3_hash_update(&(ctx), d, l) +#define ssh2_sha256_final(ctx, out) ssh2_os400qc3_hash_final(&(ctx), out) +#define ssh2_sha256(d, l, out) ssh2_os400qc3_hash(d, l, out, Qc3_SHA256) +#define ssh2_sha384_init(x) ssh2_os400qc3_hash_init(x, Qc3_SHA384) +#define ssh2_sha384_update(ctx, d, l) ssh2_os400qc3_hash_update(&(ctx), d, l) +#define ssh2_sha384_final(ctx, out) ssh2_os400qc3_hash_final(&(ctx), out) +#define ssh2_sha384(d, l, out) ssh2_os400qc3_hash(d, l, out, Qc3_SHA384) +#define ssh2_sha512_init(x) ssh2_os400qc3_hash_init(x, Qc3_SHA512) +#define ssh2_sha512_update(ctx, d, l) ssh2_os400qc3_hash_update(&(ctx), d, l) +#define ssh2_sha512_final(ctx, out) ssh2_os400qc3_hash_final(&(ctx), out) +#define ssh2_sha512(d, l, out) ssh2_os400qc3_hash(d, l, out, Qc3_SHA512) #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM -#define MD5_DIGEST_LENGTH 16 -#define libssh2_md5_ctx Qc3_Format_ALGD0100_T -#define libssh2_md5_init(x) \ - _libssh2_os400qc3_hash_init(x, Qc3_MD5) -#define libssh2_md5_update(ctx, data, len) \ - _libssh2_os400qc3_hash_update(&(ctx), data, len) -#define libssh2_md5_final(ctx, out) \ - _libssh2_os400qc3_hash_final(&(ctx), out) +#define MD5_DIGEST_LENGTH 16 +#define ssh2_md5_ctx Qc3_Format_ALGD0100_T +#define ssh2_md5_init(x) ssh2_os400qc3_hash_init(x, Qc3_MD5) +#define ssh2_md5_update(ctx, d, l) ssh2_os400qc3_hash_update(&(ctx), d, l) +#define ssh2_md5_final(ctx, out) ssh2_os400qc3_hash_final(&(ctx), out) #endif -#define libssh2_bn_ctx int /* Not used. */ +#define ssh2_bn_ctx int /* Not used. */ -#define _libssh2_bn_ctx_new() 0 -#define _libssh2_bn_ctx_free(bnctx) ((void)0) +#define ssh2_bn_ctx_new() 0 +#define ssh2_bn_ctx_free(bnctx) ((void)0) -#define _libssh2_bn_init_from_bin() _libssh2_bn_init() -#define _libssh2_bn_bytes(bn) ((bn)->length) +#define ssh2_bn_init_from_bin() ssh2_bn_init() +#define ssh2_bn_bytes(bn) ((bn)->length) -#define LIBSSH2_CIPHER_T(name) struct os400qc3_cipher name -#define libssh2_cipher_aes128 \ - { Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CBC, 16 } -#define libssh2_cipher_aes192 \ - { Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CBC, 24 } -#define libssh2_cipher_aes256 \ - { Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CBC, 32 } -#define libssh2_cipher_aes128ctr \ - { Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CTR, 16 } -#define libssh2_cipher_aes192ctr \ - { Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CTR, 24 } -#define libssh2_cipher_aes256ctr \ - { Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CTR, 32 } -#define libssh2_cipher_3des \ - { Qc3_Alg_Block_Cipher, Qc3_TDES, 8, Qc3_CBC, 24 } +#define SSH2_CIPHER_T(name) struct os400qc3_cipher name +#define ssh2_cipher_aes128 {Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CBC, 16} +#define ssh2_cipher_aes192 {Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CBC, 24} +#define ssh2_cipher_aes256 {Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CBC, 32} +#define ssh2_cipher_aes128ctr {Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CTR, 16} +#define ssh2_cipher_aes192ctr {Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CTR, 24} +#define ssh2_cipher_aes256ctr {Qc3_Alg_Block_Cipher, Qc3_AES, 16, Qc3_CTR, 32} +#define ssh2_cipher_3des {Qc3_Alg_Block_Cipher, Qc3_TDES, 8, Qc3_CBC, 24} /* Nonsense values for chacha20-poly1305 */ -#define libssh2_cipher_chacha20 \ - { Qc3_Alg_Stream_Cipher, Qc3_RC4, 8, 0, 16 } -#define libssh2_cipher_arcfour \ - { Qc3_Alg_Stream_Cipher, Qc3_RC4, 8, 0, 16 } +#define ssh2_cipher_chacha20 {Qc3_Alg_Stream_Cipher, Qc3_RC4, 8, 0, 16} +#define ssh2_cipher_arcfour {Qc3_Alg_Stream_Cipher, Qc3_RC4, 8, 0, 16} -#define _libssh2_cipher_dtor(ctx) _libssh2_os400qc3_crypto_dtor(ctx) +#define ssh2_cipher_dtor(ctx) ssh2_os400qc3_crypto_dtor(ctx) -#define libssh2_rsa_ctx struct os400qc3_crypto_ctx -#define _libssh2_rsa_free(ctx) \ - (_libssh2_os400qc3_crypto_dtor(ctx), free((char *)ctx)) -#define libssh2_prepare_iovec(vec, len) memset((char *)(vec), 0, \ - (len) * sizeof(struct iovec)) -#define _libssh2_rsa_sha1_signv(session, sig, siglen, count, vector, ctx) \ - _libssh2_os400qc3_rsa_signv(session, Qc3_SHA1, sig, siglen, \ - count, vector, ctx) -#define _libssh2_rsa_sha2_256_signv(session, sig, siglen, cnt, vector, ctx) \ - _libssh2_os400qc3_rsa_signv(session, Qc3_SHA256, sig, siglen, \ - cnt, vector, ctx) -#define _libssh2_rsa_sha2_512_signv(session, sig, siglen, cnt, vector, ctx) \ - _libssh2_os400qc3_rsa_signv(session, Qc3_SHA512, sig, siglen, \ - cnt, vector, ctx) +#define ssh2_rsa_ctx struct os400qc3_crypto_ctx +#define ssh2_rsa_free(ctx) \ + (ssh2_os400qc3_crypto_dtor(ctx), free((char *)ctx)) +#define ssh2_prepare_iovec(vec, len) \ + memset((char *)(vec), 0, (len) * sizeof(struct iovec)) +#define ssh2_rsa_sha1_signv(session, sig, siglen, count, vector, ctx) \ + ssh2_os400qc3_rsa_signv(session, Qc3_SHA1, sig, siglen, count, vector, ctx) +#define ssh2_rsa_sha2_256_signv(session, sig, siglen, cnt, vector, ctx) \ + ssh2_os400qc3_rsa_signv(session, Qc3_SHA256, sig, siglen, cnt, vector, ctx) +#define ssh2_rsa_sha2_512_signv(session, sig, siglen, cnt, vector, ctx) \ + ssh2_os400qc3_rsa_signv(session, Qc3_SHA512, sig, siglen, cnt, vector, ctx) /* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 Qc3 is limited to a maximum 2048-bit modulus/key size. */ -#define LIBSSH2_DH_GEX_MINGROUP 1024 -#define LIBSSH2_DH_GEX_OPTGROUP 1536 -#define LIBSSH2_DH_GEX_MAXGROUP 2048 +#define SSH2_DH_GEX_MINGROUP 1024 +#define SSH2_DH_GEX_OPTGROUP 1536 +#define SSH2_DH_GEX_MAXGROUP 2048 -#define LIBSSH2_DH_MAX_MODULUS_BITS 2048 +#define SSH2_DH_MAX_MODULUS_BITS 2048 -#define libssh2_dh_ctx struct os400qc3_dh_ctx -#define libssh2_dh_init(dhctx) _libssh2_os400qc3_dh_init(dhctx) -#define libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ - _libssh2_os400qc3_dh_key_pair(dhctx, public, g, p, group_order) -#define libssh2_dh_secret(dhctx, secret, f, p, bnctx) \ - _libssh2_os400qc3_dh_secret(dhctx, secret, f, p) -#define libssh2_dh_dtor(dhctx) _libssh2_os400qc3_dh_dtor(dhctx) +#define ssh2_dh_ctx struct os400qc3_dh_ctx +#define ssh2_dh_init(dhctx) ssh2_os400qc3_dh_init(dhctx) +#define ssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ + ssh2_os400qc3_dh_key_pair(dhctx, public, g, p, group_order) +#define ssh2_dh_secret(dhctx, secret, f, p, bnctx) \ + ssh2_os400qc3_dh_secret(dhctx, secret, f, p) +#define ssh2_dh_dtor(dhctx) ssh2_os400qc3_dh_dtor(dhctx) /******************************************************************* * @@ -359,40 +330,36 @@ struct os400qc3_dh_ctx { /* Diffie-Hellman context. */ * *******************************************************************/ -libssh2_bn *_libssh2_bn_init(void); -void_libssh2_bn_free(libssh2_bn *bn); -unsigned long _libssh2_bn_bits(libssh2_bn *bn); -int _libssh2_bn_from_bin(libssh2_bn *bn, size_t len, - const unsigned char *v); -int _libssh2_bn_set_word(libssh2_bn *bn, unsigned long val); -int _libssh2_bn_to_bin(libssh2_bn *bn, unsigned char *val); -int _libssh2_random(unsigned char *buf, size_t len); -void _libssh2_os400qc3_crypto_dtor(struct os400qc3_crypto_ctx *x); -int _libssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, - unsigned int algo); -int _libssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx, - const unsigned char *data, - int len); -int _libssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, - unsigned char *out); -int _libssh2_os400qc3_hash(const unsigned char *message, - unsigned long len, unsigned char *out, - unsigned int algo); -int _libssh2_os400qc3_rsa_signv(LIBSSH2_SESSION *session, int algo, - unsigned char **signature, - size_t *signature_len, - int veccount, - const struct iovec vector[], - libssh2_rsa_ctx *ctx); -void _libssh2_os400qc3_dh_init(libssh2_dh_ctx *dhctx); -int _libssh2_os400qc3_dh_key_pair(libssh2_dh_ctx *dhctx, - libssh2_bn *public, - libssh2_bn *g, - libssh2_bn *p, int group_order); -int _libssh2_os400qc3_dh_secret(libssh2_dh_ctx *dhctx, - libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p); -void _libssh2_os400qc3_dh_dtor(libssh2_dh_ctx *dhctx); +ssh2_bn *ssh2_bn_init(void); +voidssh2_bn_free(ssh2_bn *bn); +unsigned long ssh2_bn_bits(ssh2_bn *bn); +int ssh2_bn_from_bin(ssh2_bn *bn, size_t len, const unsigned char *v); +int ssh2_bn_set_word(ssh2_bn *bn, unsigned long val); +int ssh2_bn_to_bin(ssh2_bn *bn, unsigned char *val); +int ssh2_random(unsigned char *buf, size_t len); +void ssh2_os400qc3_crypto_dtor(struct os400qc3_crypto_ctx *x); +int ssh2_os400qc3_hash_init(Qc3_Format_ALGD0100_T *x, unsigned int algo); +int ssh2_os400qc3_hash_update(Qc3_Format_ALGD0100_T *ctx, + const unsigned char *data, int len); +int ssh2_os400qc3_hash_final(Qc3_Format_ALGD0100_T *ctx, unsigned char *out); +int ssh2_os400qc3_hash(const unsigned char *message, + unsigned long len, unsigned char *out, + unsigned int algo); +int ssh2_os400qc3_rsa_signv(LIBSSH2_SESSION *session, int algo, + unsigned char **signature, + size_t *signature_len, + int veccount, + const struct iovec vector[], + ssh2_rsa_ctx *ctx); +void ssh2_os400qc3_dh_init(ssh2_dh_ctx *dhctx); +int ssh2_os400qc3_dh_key_pair(ssh2_dh_ctx *dhctx, + ssh2_bn *public, + ssh2_bn *g, + ssh2_bn *p, int group_order); +int ssh2_os400qc3_dh_secret(ssh2_dh_ctx *dhctx, + ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p); +void ssh2_os400qc3_dh_dtor(ssh2_dh_ctx *dhctx); #endif /* LIBSSH2_OS400QC3_H */ diff --git a/src/packet.c b/src/packet.c index bd6ae92d..712f37b9 100644 --- a/src/packet.c +++ b/src/packet.c @@ -71,11 +71,11 @@ static inline int packet_queue_listener( /* 17 = packet_type(1) + channel(4) + reason(4) + descr(4) + lang(4) */ size_t packet_len = 17 + strlen(FwdNotReq); unsigned char *p; - LIBSSH2_LISTENER *listn = _libssh2_list_first(&session->listeners); + LIBSSH2_LISTENER *listn = ssh2_list_first(&session->listeners); uint32_t failure_code = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; int rc; - if(listen_state->state == libssh2_NB_state_idle) { + if(listen_state->state == ssh2_NB_state_idle) { size_t offset = strlen("forwarded-tcpip") + 5; size_t temp_len = 0; struct string_buf buf; @@ -84,56 +84,56 @@ static inline int packet_queue_listener( buf.len = datalen; if(datalen < offset + 12) { /* 3 * 4-byte */ - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Unexpected packet size"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Unexpected packet size"); } buf.dataptr += offset; - if(_libssh2_get_u32(&buf, &(listen_state->sender_channel))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting channel"); + if(ssh2_get_u32(&buf, &(listen_state->sender_channel))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting channel"); } - if(_libssh2_get_u32(&buf, &(listen_state->initial_window_size))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting window size"); + if(ssh2_get_u32(&buf, &(listen_state->initial_window_size))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting window size"); } - if(_libssh2_get_u32(&buf, &(listen_state->packet_size))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting packet"); + if(ssh2_get_u32(&buf, &(listen_state->packet_size))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting packet"); } - if(_libssh2_get_string(&buf, &(listen_state->host), &temp_len)) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting host"); + if(ssh2_get_string(&buf, &(listen_state->host), &temp_len)) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting host"); } listen_state->host_len = (uint32_t)temp_len; - if(_libssh2_get_u32(&buf, &(listen_state->port))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting port"); + if(ssh2_get_u32(&buf, &(listen_state->port))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting port"); } - if(_libssh2_get_string(&buf, &(listen_state->shost), &temp_len)) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting shost"); + if(ssh2_get_string(&buf, &(listen_state->shost), &temp_len)) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting shost"); } listen_state->shost_len = (uint32_t)temp_len; - if(_libssh2_get_u32(&buf, &(listen_state->sport))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting sport"); + if(ssh2_get_u32(&buf, &(listen_state->sport))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting sport"); } - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Remote received connection from %.*s:%u to %.*s:%u", - listen_state->shost_len, listen_state->shost, - listen_state->sport, - listen_state->host_len, listen_state->host, - listen_state->port)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Remote received connection from %.*s:%u to %.*s:%u", + listen_state->shost_len, listen_state->shost, + listen_state->sport, + listen_state->host_len, listen_state->host, + listen_state->port)); - listen_state->state = libssh2_NB_state_allocated; + listen_state->state = ssh2_NB_state_allocated; } - if(listen_state->state != libssh2_NB_state_sent) { + if(listen_state->state != ssh2_NB_state_sent) { while(listn) { if((listn->port == (int)listen_state->port) && (strlen(listn->host) == listen_state->host_len) && @@ -143,41 +143,39 @@ static inline int packet_queue_listener( LIBSSH2_CHANNEL *channel = NULL; listen_state->channel = NULL; - if(listen_state->state == libssh2_NB_state_allocated) { + if(listen_state->state == ssh2_NB_state_allocated) { if(listn->queue_maxsize && (listn->queue_maxsize <= listn->queue_size)) { /* Queue is full */ failure_code = SSH_OPEN_RESOURCE_SHORTAGE; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Listener queue full, ignoring")); - listen_state->state = libssh2_NB_state_sent; + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Listener queue full, ignoring")); + listen_state->state = ssh2_NB_state_sent; break; } - channel = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); + channel = SSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if(!channel) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate a channel for " - "new connection"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate a channel for " + "new connection"); failure_code = SSH_OPEN_RESOURCE_SHORTAGE; - listen_state->state = libssh2_NB_state_sent; + listen_state->state = ssh2_NB_state_sent; break; } listen_state->channel = channel; channel->session = session; channel->channel_type_len = strlen("forwarded-tcpip"); - channel->channel_type = LIBSSH2_ALLOC(session, - channel-> - channel_type_len + - 1); + channel->channel_type = + SSH2_ALLOC(session, channel->channel_type_len + 1); if(!channel->channel_type) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate a channel for new" - " connection"); - LIBSSH2_FREE(session, channel); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate a channel for " + "new connection"); + SSH2_FREE(session, channel); failure_code = SSH_OPEN_RESOURCE_SHORTAGE; - listen_state->state = libssh2_NB_state_sent; + listen_state->state = ssh2_NB_state_sent; break; } memcpy(channel->channel_type, "forwarded-tcpip", @@ -191,91 +189,89 @@ static inline int packet_queue_listener( channel->remote.packet_size = LIBSSH2_CHANNEL_PACKET_DEFAULT; - channel->local.id = _libssh2_channel_nextid(session); + channel->local.id = ssh2_channel_nextid(session); channel->local.window_size_initial = listen_state->initial_window_size; channel->local.window_size = listen_state->initial_window_size; channel->local.packet_size = listen_state->packet_size; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Connection queued: channel %u/%u " - "win %u/%u packet %u/%u", - channel->local.id, channel->remote.id, - channel->local.window_size, - channel->remote.window_size, - channel->local.packet_size, - channel->remote.packet_size)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Connection queued: channel %u/%u " + "win %u/%u packet %u/%u", + channel->local.id, channel->remote.id, + channel->local.window_size, + channel->remote.window_size, + channel->local.packet_size, + channel->remote.packet_size)); p = listen_state->packet; *(p++) = SSH_MSG_CHANNEL_OPEN_CONFIRMATION; - _libssh2_store_u32(&p, channel->remote.id); - _libssh2_store_u32(&p, channel->local.id); - _libssh2_store_u32(&p, - channel->remote.window_size_initial); - _libssh2_store_u32(&p, channel->remote.packet_size); + ssh2_store_u32(&p, channel->remote.id); + ssh2_store_u32(&p, channel->local.id); + ssh2_store_u32(&p, channel->remote.window_size_initial); + ssh2_store_u32(&p, channel->remote.packet_size); - listen_state->state = libssh2_NB_state_created; + listen_state->state = ssh2_NB_state_created; } - if(listen_state->state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, listen_state->packet, - 17, NULL, 0); + if(listen_state->state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, listen_state->packet, + 17, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; else if(rc) { - listen_state->state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Unable to send channel " - "open confirmation"); + listen_state->state = ssh2_NB_state_idle; + return ssh2_err(session, rc, + "Unable to send channel " + "open confirmation"); } /* Link the channel into the end of the queue list */ if(listen_state->channel) { - _libssh2_list_add(&listn->queue, - &listen_state->channel->node); + ssh2_list_add(&listn->queue, + &listen_state->channel->node); listn->queue_size++; } - listen_state->state = libssh2_NB_state_idle; + listen_state->state = ssh2_NB_state_idle; return 0; } } - listn = _libssh2_list_next(&listn->node); + listn = ssh2_list_next(&listn->node); } - listen_state->state = libssh2_NB_state_sent; + listen_state->state = ssh2_NB_state_sent; } /* We are not listening to you */ p = listen_state->packet; *(p++) = SSH_MSG_CHANNEL_OPEN_FAILURE; - _libssh2_store_u32(&p, listen_state->sender_channel); - _libssh2_store_u32(&p, failure_code); - _libssh2_store_str(&p, FwdNotReq, strlen(FwdNotReq)); - _libssh2_htonu32(p, 0); + ssh2_store_u32(&p, listen_state->sender_channel); + ssh2_store_u32(&p, failure_code); + ssh2_store_str(&p, FwdNotReq, strlen(FwdNotReq)); + ssh2_htonu32(p, 0); - rc = _libssh2_transport_send(session, listen_state->packet, - packet_len, NULL, 0); + rc = ssh2_transport_send(session, listen_state->packet, + packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - listen_state->state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Unable to send open failure"); + listen_state->state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send open failure"); } - listen_state->state = libssh2_NB_state_idle; + listen_state->state = ssh2_NB_state_idle; return 0; } /* * Accept a forwarded X11 connection */ -static inline int packet_x11_open( - LIBSSH2_SESSION *session, - unsigned char *data, size_t datalen, - struct packet_x11_open_state *x11open_state) +static inline int packet_x11_open(LIBSSH2_SESSION *session, + unsigned char *data, size_t datalen, + struct packet_x11_open_state *x11open_state) { uint32_t failure_code = SSH_OPEN_CONNECT_FAILED; /* 17 = packet_type(1) + channel(4) + reason(4) + descr(4) + lang(4) */ @@ -284,7 +280,7 @@ static inline int packet_x11_open( LIBSSH2_CHANNEL *channel = x11open_state->channel; int rc; - if(x11open_state->state == libssh2_NB_state_idle) { + if(x11open_state->state == ssh2_NB_state_idle) { size_t offset = strlen("x11") + 5; size_t temp_len = 0; @@ -295,85 +291,79 @@ static inline int packet_x11_open( buf.len = datalen; if(datalen < offset + 12) { /* 3 * 4-byte */ - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "unexpected data length"); + ssh2_err(session, LIBSSH2_ERROR_INVAL, "unexpected data length"); failure_code = SSH_OPEN_CONNECT_FAILED; goto x11_exit; } buf.dataptr += offset; - if(_libssh2_get_u32(&buf, &(x11open_state->sender_channel))) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "unexpected sender channel size"); + if(ssh2_get_u32(&buf, &(x11open_state->sender_channel))) { + ssh2_err(session, LIBSSH2_ERROR_INVAL, + "unexpected sender channel size"); failure_code = SSH_OPEN_CONNECT_FAILED; goto x11_exit; } - if(_libssh2_get_u32(&buf, &(x11open_state->initial_window_size))) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "unexpected window size"); + if(ssh2_get_u32(&buf, &(x11open_state->initial_window_size))) { + ssh2_err(session, LIBSSH2_ERROR_INVAL, "unexpected window size"); failure_code = SSH_OPEN_CONNECT_FAILED; goto x11_exit; } - if(_libssh2_get_u32(&buf, &(x11open_state->packet_size))) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "unexpected packet size"); + if(ssh2_get_u32(&buf, &(x11open_state->packet_size))) { + ssh2_err(session, LIBSSH2_ERROR_INVAL, "unexpected packet size"); failure_code = SSH_OPEN_CONNECT_FAILED; goto x11_exit; } - if(_libssh2_get_string(&buf, &temp_buf, &temp_len)) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "unexpected host size"); + if(ssh2_get_string(&buf, &temp_buf, &temp_len)) { + ssh2_err(session, LIBSSH2_ERROR_INVAL, "unexpected host size"); failure_code = SSH_OPEN_CONNECT_FAILED; goto x11_exit; } x11open_state->shost_len = (uint32_t)temp_len; - x11open_state->shost = LIBSSH2_ALLOC(session, - x11open_state->shost_len + 1); + x11open_state->shost = + SSH2_ALLOC(session, x11open_state->shost_len + 1); if(!x11open_state->shost) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for shost"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for shost"); failure_code = SSH_OPEN_CONNECT_FAILED; goto x11_exit; } memcpy(x11open_state->shost, temp_buf, x11open_state->shost_len); x11open_state->shost[x11open_state->shost_len] = '\0'; - if(_libssh2_get_u32(&buf, &(x11open_state->sport))) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "unexpected port size"); + if(ssh2_get_u32(&buf, &(x11open_state->sport))) { + ssh2_err(session, LIBSSH2_ERROR_INVAL, "unexpected port size"); failure_code = SSH_OPEN_CONNECT_FAILED; goto x11_exit; } - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "X11 Connection Received from %s:%u on channel %u", - x11open_state->shost, x11open_state->sport, - x11open_state->sender_channel)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "X11 Connection Received from %s:%u on channel %u", + x11open_state->shost, x11open_state->sport, + x11open_state->sender_channel)); - x11open_state->state = libssh2_NB_state_allocated; + x11open_state->state = ssh2_NB_state_allocated; } if(session->x11) { - if(x11open_state->state == libssh2_NB_state_allocated) { - channel = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); + if(x11open_state->state == ssh2_NB_state_allocated) { + channel = SSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if(!channel) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "allocate a channel for new connection"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "allocate a channel for new connection"); failure_code = SSH_OPEN_RESOURCE_SHORTAGE; goto x11_exit; } channel->session = session; channel->channel_type_len = strlen("x11"); - channel->channel_type = LIBSSH2_ALLOC(session, - channel->channel_type_len + - 1); + channel->channel_type = + SSH2_ALLOC(session, channel->channel_type_len + 1); if(!channel->channel_type) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "allocate a channel for new connection"); - LIBSSH2_FREE(session, channel); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "allocate a channel for new connection"); + SSH2_FREE(session, channel); failure_code = SSH_OPEN_RESOURCE_SHORTAGE; goto x11_exit; } @@ -386,58 +376,57 @@ static inline int packet_x11_open( channel->remote.window_size = LIBSSH2_CHANNEL_WINDOW_DEFAULT; channel->remote.packet_size = LIBSSH2_CHANNEL_PACKET_DEFAULT; - channel->local.id = _libssh2_channel_nextid(session); + channel->local.id = ssh2_channel_nextid(session); channel->local.window_size_initial = x11open_state->initial_window_size; channel->local.window_size = x11open_state->initial_window_size; channel->local.packet_size = x11open_state->packet_size; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "X11 Connection established: channel %u/%u " - "win %u/%u packet %u/%u", - channel->local.id, channel->remote.id, - channel->local.window_size, - channel->remote.window_size, - channel->local.packet_size, - channel->remote.packet_size)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "X11 Connection established: channel %u/%u " + "win %u/%u packet %u/%u", + channel->local.id, channel->remote.id, + channel->local.window_size, + channel->remote.window_size, + channel->local.packet_size, + channel->remote.packet_size)); p = x11open_state->packet; *(p++) = SSH_MSG_CHANNEL_OPEN_CONFIRMATION; - _libssh2_store_u32(&p, channel->remote.id); - _libssh2_store_u32(&p, channel->local.id); - _libssh2_store_u32(&p, channel->remote.window_size_initial); - _libssh2_store_u32(&p, channel->remote.packet_size); + ssh2_store_u32(&p, channel->remote.id); + ssh2_store_u32(&p, channel->local.id); + ssh2_store_u32(&p, channel->remote.window_size_initial); + ssh2_store_u32(&p, channel->remote.packet_size); - x11open_state->state = libssh2_NB_state_created; + x11open_state->state = ssh2_NB_state_created; } - if(x11open_state->state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, x11open_state->packet, 17, - NULL, 0); + if(x11open_state->state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, x11open_state->packet, 17, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - LIBSSH2_FREE(session, x11open_state->shost); + SSH2_FREE(session, x11open_state->shost); x11open_state->shost = NULL; - x11open_state->state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send channel open " - "confirmation"); + x11open_state->state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send channel open confirmation"); } /* Link the channel into the session */ - _libssh2_list_add(&session->channels, &channel->node); + ssh2_list_add(&session->channels, &channel->node); /* * Pass control to the callback, they may turn right around and * free the channel, or actually use it */ - LIBSSH2_X11_OPEN(channel, (char *)x11open_state->shost, - x11open_state->sport); + SSH2_X11_OPEN(channel, (char *)x11open_state->shost, + x11open_state->sport); - LIBSSH2_FREE(session, x11open_state->shost); + SSH2_FREE(session, x11open_state->shost); x11open_state->shost = NULL; - x11open_state->state = libssh2_NB_state_idle; + x11open_state->state = ssh2_NB_state_idle; return 0; } } @@ -445,26 +434,26 @@ static inline int packet_x11_open( failure_code = SSH_OPEN_RESOURCE_SHORTAGE; /* fall-through */ x11_exit: - LIBSSH2_FREE(session, x11open_state->shost); + SSH2_FREE(session, x11open_state->shost); x11open_state->shost = NULL; p = x11open_state->packet; *(p++) = SSH_MSG_CHANNEL_OPEN_FAILURE; - _libssh2_store_u32(&p, x11open_state->sender_channel); - _libssh2_store_u32(&p, failure_code); - _libssh2_store_str(&p, X11FwdUnAvil, strlen(X11FwdUnAvil)); - _libssh2_htonu32(p, 0); + ssh2_store_u32(&p, x11open_state->sender_channel); + ssh2_store_u32(&p, failure_code); + ssh2_store_str(&p, X11FwdUnAvil, strlen(X11FwdUnAvil)); + ssh2_htonu32(p, 0); - rc = _libssh2_transport_send(session, x11open_state->packet, packet_len, - NULL, 0); + rc = ssh2_transport_send(session, x11open_state->packet, packet_len, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - x11open_state->state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Unable to send open failure"); + x11open_state->state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send open failure"); } - x11open_state->state = libssh2_NB_state_idle; + x11open_state->state = ssh2_NB_state_idle; return 0; } @@ -490,54 +479,53 @@ static inline int packet_authagent_open( buf.len = datalen; if(datalen < offset + 12) { /* 27-byte header + 3 * 4-byte */ - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Unexpected packet size"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Unexpected packet size"); } buf.dataptr += offset; - if(authagent_state->state == libssh2_NB_state_idle) { - if(_libssh2_get_u32(&buf, &(authagent_state->sender_channel))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting channel"); + if(authagent_state->state == ssh2_NB_state_idle) { + if(ssh2_get_u32(&buf, &(authagent_state->sender_channel))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting channel"); } - if(_libssh2_get_u32(&buf, &(authagent_state->initial_window_size))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting window size"); + if(ssh2_get_u32(&buf, &(authagent_state->initial_window_size))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting window size"); } - if(_libssh2_get_u32(&buf, &(authagent_state->packet_size))) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting packet"); + if(ssh2_get_u32(&buf, &(authagent_state->packet_size))) { + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting packet"); } - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Auth Agent Connection Received on channel %u", - authagent_state->sender_channel)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Auth Agent Connection Received on channel %u", + authagent_state->sender_channel)); - authagent_state->state = libssh2_NB_state_allocated; + authagent_state->state = ssh2_NB_state_allocated; } if(session->authagent) { - if(authagent_state->state == libssh2_NB_state_allocated) { - channel = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); + if(authagent_state->state == ssh2_NB_state_allocated) { + channel = SSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); authagent_state->channel = channel; if(!channel) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "allocate a channel for new connection"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "allocate a channel for new connection"); failure_code = SSH_OPEN_RESOURCE_SHORTAGE; goto authagent_exit; } channel->session = session; channel->channel_type_len = strlen("auth agent"); - channel->channel_type = LIBSSH2_ALLOC(session, - channel->channel_type_len + - 1); + channel->channel_type = + SSH2_ALLOC(session, channel->channel_type_len + 1); if(!channel->channel_type) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "allocate a channel for new connection"); - LIBSSH2_FREE(session, channel); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "allocate a channel for new connection"); + SSH2_FREE(session, channel); failure_code = SSH_OPEN_RESOURCE_SHORTAGE; goto authagent_exit; } @@ -550,58 +538,57 @@ static inline int packet_authagent_open( channel->remote.window_size = LIBSSH2_CHANNEL_WINDOW_DEFAULT; channel->remote.packet_size = LIBSSH2_CHANNEL_PACKET_DEFAULT; - channel->local.id = _libssh2_channel_nextid(session); + channel->local.id = ssh2_channel_nextid(session); channel->local.window_size_initial = authagent_state->initial_window_size; channel->local.window_size = authagent_state->initial_window_size; channel->local.packet_size = authagent_state->packet_size; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Auth Agent Connection established: channel " - "%u/%u win %u/%u packet %u/%u", - channel->local.id, channel->remote.id, - channel->local.window_size, - channel->remote.window_size, - channel->local.packet_size, - channel->remote.packet_size)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Auth Agent Connection established: channel " + "%u/%u win %u/%u packet %u/%u", + channel->local.id, channel->remote.id, + channel->local.window_size, + channel->remote.window_size, + channel->local.packet_size, + channel->remote.packet_size)); p = authagent_state->packet; *(p++) = SSH_MSG_CHANNEL_OPEN_CONFIRMATION; - _libssh2_store_u32(&p, channel->remote.id); - _libssh2_store_u32(&p, channel->local.id); - _libssh2_store_u32(&p, channel->remote.window_size_initial); - _libssh2_store_u32(&p, channel->remote.packet_size); + ssh2_store_u32(&p, channel->remote.id); + ssh2_store_u32(&p, channel->local.id); + ssh2_store_u32(&p, channel->remote.window_size_initial); + ssh2_store_u32(&p, channel->remote.packet_size); - authagent_state->state = libssh2_NB_state_created; + authagent_state->state = ssh2_NB_state_created; } - if(authagent_state->state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, authagent_state->packet, 17, - NULL, 0); + if(authagent_state->state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, authagent_state->packet, 17, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - authagent_state->state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send channel open " - "confirmation"); + authagent_state->state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send channel open confirmation"); } /* Link the channel into the session */ - _libssh2_list_add(&session->channels, &channel->node); + ssh2_list_add(&session->channels, &channel->node); /* mess with stuff so we do not keep reading the same packet over and over */ session->packet.total_num = 0; - session->fullpacket_state = libssh2_NB_state_idle; + session->fullpacket_state = ssh2_NB_state_idle; /* Pass control to the callback, they may turn right around and and free the channel, or actually use it */ - LIBSSH2_AUTHAGENT(channel); + SSH2_AUTHAGENT(channel); - authagent_state->state = libssh2_NB_state_idle; + authagent_state->state = ssh2_NB_state_idle; return 0; } } @@ -612,21 +599,21 @@ static inline int packet_authagent_open( authagent_exit: p = authagent_state->packet; *(p++) = SSH_MSG_CHANNEL_OPEN_FAILURE; - _libssh2_store_u32(&p, authagent_state->sender_channel); - _libssh2_store_u32(&p, failure_code); - _libssh2_store_str(&p, AuthAgentUnavail, strlen(AuthAgentUnavail)); - _libssh2_htonu32(p, 0); + ssh2_store_u32(&p, authagent_state->sender_channel); + ssh2_store_u32(&p, failure_code); + ssh2_store_str(&p, AuthAgentUnavail, strlen(AuthAgentUnavail)); + ssh2_htonu32(p, 0); - rc = _libssh2_transport_send(session, authagent_state->packet, packet_len, - NULL, 0); + rc = ssh2_transport_send(session, authagent_state->packet, packet_len, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { return rc; } else if(rc) { - authagent_state->state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Unable to send open failure"); + authagent_state->state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Unable to send open failure"); } - authagent_state->state = libssh2_NB_state_idle; + authagent_state->state = ssh2_NB_state_idle; return 0; } @@ -639,8 +626,8 @@ authagent_exit: * * This function is always called with 'datalen' greater than zero. */ -int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, - size_t datalen, int macstate, uint32_t seq) +int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, + size_t datalen, int macstate, uint32_t seq) { int rc = 0; unsigned char *message = NULL; @@ -656,48 +643,48 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, unsigned char want_reply = 0; switch(session->packAdd_state) { - case libssh2_NB_state_idle: - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Packet type %u received, length=%ld", - (unsigned int)msg, (long)datalen)); + case ssh2_NB_state_idle: + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "Packet type %u received, length=%ld", + (unsigned int)msg, (long)datalen)); - if((macstate == LIBSSH2_MAC_INVALID) && + if((macstate == SSH2_MAC_INVALID) && (!session->macerror || - LIBSSH2_MACERROR(session, (char *)data, datalen))) { + SSH2_MACERROR(session, (char *)data, datalen))) { /* Bad MAC input, but no callback set or non-zero return from the callback */ - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_INVALID_MAC, - "Invalid MAC received"); + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_INVALID_MAC, + "Invalid MAC received"); } - session->packAdd_state = libssh2_NB_state_allocated; + session->packAdd_state = ssh2_NB_state_allocated; break; - case libssh2_NB_state_jump1: - goto libssh2_packet_add_jump_point1; - case libssh2_NB_state_jump2: - goto libssh2_packet_add_jump_point2; - case libssh2_NB_state_jump3: - goto libssh2_packet_add_jump_point3; - case libssh2_NB_state_jump4: - goto libssh2_packet_add_jump_point4; - case libssh2_NB_state_jump5: - goto libssh2_packet_add_jump_point5; - case libssh2_NB_state_jumpauthagent: - goto libssh2_packet_add_jump_authagent; + case ssh2_NB_state_jump1: + goto ssh2_packet_add_jump_point1; + case ssh2_NB_state_jump2: + goto ssh2_packet_add_jump_point2; + case ssh2_NB_state_jump3: + goto ssh2_packet_add_jump_point3; + case ssh2_NB_state_jump4: + goto ssh2_packet_add_jump_point4; + case ssh2_NB_state_jump5: + goto ssh2_packet_add_jump_point5; + case ssh2_NB_state_jumpauthagent: + goto ssh2_packet_add_jump_authagent; default: /* nothing to do */ break; } - if(session->state & LIBSSH2_STATE_INITIAL_KEX) { + if(session->state & SSH2_STATE_INITIAL_KEX) { if(msg == SSH_MSG_KEXINIT) { if(!session->kex_strict) { if(datalen < 17) { - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; - return _libssh2_error(session, - LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short extracting kex"); + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; + return ssh2_err(session, + LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short extracting kex"); } else { const unsigned char *strict = @@ -711,49 +698,49 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, buf.len = datalen; buf.dataptr += 17; /* advance past type and cookie */ - if(_libssh2_get_string(&buf, &algs, &algs_len)) { - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; - return _libssh2_error(session, - LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Algs too short"); + if(ssh2_get_string(&buf, &algs, &algs_len)) { + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; + return ssh2_err(session, + LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Algs too short"); } if(algs_len == 0 || - _libssh2_kex_agree_instr(algs, algs_len, strict, 28)) { + ssh2_kex_agree_instr(algs, algs_len, strict, 28)) { session->kex_strict = 1; } } } if(session->kex_strict && seq) { - LIBSSH2_FREE(session, data); - session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->socket_state = SSH2_SOCKET_DISCONNECTED; + session->packAdd_state = ssh2_NB_state_idle; libssh2_session_disconnect(session, "strict KEX violation: " "KEXINIT was not the first packet"); - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "strict KEX violation: " - "KEXINIT was not the first packet"); + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "strict KEX violation: " + "KEXINIT was not the first packet"); } } if(session->kex_strict && session->fullpacket_required_type && session->fullpacket_required_type != msg) { - LIBSSH2_FREE(session, data); - session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->socket_state = SSH2_SOCKET_DISCONNECTED; + session->packAdd_state = ssh2_NB_state_idle; libssh2_session_disconnect(session, "strict KEX violation: " "unexpected packet type"); - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "strict KEX violation: " - "unexpected packet type"); + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "strict KEX violation: " + "unexpected packet type"); } } - if(session->packAdd_state == libssh2_NB_state_allocated) { + if(session->packAdd_state == ssh2_NB_state_allocated) { /* A couple exceptions to the packet adding rule: */ switch(msg) { @@ -773,27 +760,27 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, buf.len = datalen; buf.dataptr++; /* advance past type */ - _libssh2_get_u32(&buf, &reason); - _libssh2_get_string(&buf, &message, &message_len); - _libssh2_get_string(&buf, &language, &language_len); + ssh2_get_u32(&buf, &reason); + ssh2_get_string(&buf, &message, &message_len); + ssh2_get_string(&buf, &language, &language_len); if(session->ssh_msg_disconnect) { - LIBSSH2_DISCONNECT(session, reason, (const char *)message, - message_len, (const char *)language, - language_len); + SSH2_DISCONNECT(session, reason, (const char *)message, + message_len, (const char *)language, + language_len); } - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Disconnect(%d): %.*s(%.*s)", reason, - (int)message_len, message, - (int)language_len, language)); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "Disconnect(%d): %.*s(%.*s)", reason, + (int)message_len, message, (int)language_len, + language)); } - LIBSSH2_FREE(session, data); - session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; - session->packAdd_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "socket disconnect"); + SSH2_FREE(session, data); + session->socket_state = SSH2_SOCKET_DISCONNECTED; + session->packAdd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "socket disconnect"); /* byte SSH_MSG_IGNORE string data @@ -802,14 +789,14 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, case SSH_MSG_IGNORE: if(datalen >= 2) { if(session->ssh_msg_ignore) { - LIBSSH2_IGNORE(session, (char *)data + 1, datalen - 1); + SSH2_IGNORE(session, (char *)data + 1, datalen - 1); } } else if(session->ssh_msg_ignore) { - LIBSSH2_IGNORE(session, "", 0); + SSH2_IGNORE(session, "", 0); } - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; /* @@ -830,21 +817,21 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, buf.len = datalen; buf.dataptr += 2; /* advance past type & always display */ - _libssh2_get_string(&buf, &message, &message_len); - _libssh2_get_string(&buf, &language, &language_len); + ssh2_get_string(&buf, &message, &message_len); + ssh2_get_string(&buf, &language, &language_len); } if(session->ssh_msg_debug) { - LIBSSH2_DEBUG(session, always_display, - (const char *)message, message_len, - (const char *)language, language_len); + SSH2_DEBUG(session, always_display, (const char *)message, + message_len, (const char *)language, + language_len); } } - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Debug Packet: %.*s", (int)message_len, message)); - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Debug Packet: %.*s", + (int)message_len, message)); + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; /* @@ -864,10 +851,10 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, buf.len = datalen; buf.dataptr += 1; /* advance past type */ - if(_libssh2_get_u32(&buf, &nr_extensions) != 0 || + if(ssh2_get_u32(&buf, &nr_extensions) != 0 || nr_extensions >= 1024) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid extension info received"); + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Invalid extension info received"); } while(rc == 0 && nr_extensions > 0) { @@ -879,29 +866,26 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, nr_extensions -= 1; - if(_libssh2_get_string(&buf, &name, &name_len)) + if(ssh2_get_string(&buf, &name, &name_len)) break; - if(_libssh2_get_string(&buf, &value, &value_len)) + if(ssh2_get_string(&buf, &value, &value_len)) break; if(name && value) { - _libssh2_debug((session, LIBSSH2_TRACE_KEX, - "Server to Client extension " - "%.*s: %.*s", - (int)name_len, name, - (int)value_len, value)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, + "Server to Client extension %.*s: %.*s", + (int)name_len, name, (int)value_len, value)); } if(name && name_len == 15 && memcmp(name, "server-sig-algs", 15) == 0) { if(session->server_sign_algorithms) { - LIBSSH2_FREE(session, - session->server_sign_algorithms); + SSH2_FREE(session, + session->server_sign_algorithms); } session->server_sign_algorithms = - LIBSSH2_ALLOC(session, - value_len + 1); + SSH2_ALLOC(session, value_len + 1); if(value && session->server_sign_algorithms) { memcpy(session->server_sign_algorithms, @@ -909,15 +893,15 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, session->server_sign_algorithms[value_len] = '\0'; } else { - rc = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "memory for server sign algo"); + rc = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "memory for server sign algo"); } } } } - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return rc; /* @@ -930,27 +914,26 @@ int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, case SSH_MSG_GLOBAL_REQUEST: if(datalen >= 5) { want_reply = 0; - len = _libssh2_ntohu32(data + 1); + len = ssh2_ntohu32(data + 1); if((len <= (UINT_MAX - 6)) && (datalen >= (6 + len))) { want_reply = data[5 + len]; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Received global request type " - "%.*s (wr %X)", - (int)len, data + 5, want_reply)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Received global request type %.*s (wr %X)", + (int)len, data + 5, want_reply)); } if(want_reply) { static const unsigned char packet = SSH_MSG_REQUEST_FAILURE; -libssh2_packet_add_jump_point5: - session->packAdd_state = libssh2_NB_state_jump5; - rc = _libssh2_transport_send(session, &packet, 1, NULL, 0); +ssh2_packet_add_jump_point5: + session->packAdd_state = ssh2_NB_state_jump5; + rc = ssh2_transport_send(session, &packet, 1, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; } } - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; /* @@ -964,7 +947,7 @@ libssh2_packet_add_jump_point5: /* streamid(4) */ data_head += 4; - LIBSSH2_FALLTHROUGH(); + SSH2_FALLTHROUGH(); /* byte SSH_MSG_CHANNEL_DATA @@ -978,66 +961,65 @@ libssh2_packet_add_jump_point5: if(datalen >= data_head) channelp = - _libssh2_channel_locate(session, - _libssh2_ntohu32(data + 1)); + ssh2_channel_locate(session, ssh2_ntohu32(data + 1)); if(!channelp) { - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_UNKNOWN, - "Packet received for unknown channel"); - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_UNKNOWN, + "Packet received for unknown channel"); + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; } #ifdef LIBSSH2DEBUG { uint32_t stream_id = 0; if(msg == SSH_MSG_CHANNEL_EXTENDED_DATA) - stream_id = _libssh2_ntohu32(data + 5); + stream_id = ssh2_ntohu32(data + 5); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "%ld bytes packet_add() for %u/%u/%u", - (long)(datalen - data_head), - channelp->local.id, - channelp->remote.id, - stream_id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "%ld bytes packet_add() for %u/%u/%u", + (long)(datalen - data_head), + channelp->local.id, + channelp->remote.id, + stream_id)); } #endif if((channelp->remote.extended_data_ignore_mode == LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE) && (msg == SSH_MSG_CHANNEL_EXTENDED_DATA)) { /* Pretend we did not receive this */ - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Ignoring extended data and refunding " - "%ld bytes", (long)(datalen - 13))); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Ignoring extended data and refunding %ld bytes", + (long)(datalen - 13))); if(channelp->read_avail + datalen - data_head >= channelp->remote.window_size) datalen = channelp->remote.window_size - channelp->read_avail + data_head; - channelp->remote.window_size -= (uint32_t)(datalen - - data_head); - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "shrinking window size by %ld bytes to %u, " - "read_avail %ld", - (long)(datalen - data_head), - channelp->remote.window_size, - (long)channelp->read_avail)); + channelp->remote.window_size -= + (uint32_t)(datalen - data_head); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "shrinking window size by %ld bytes to %u, " + "read_avail %ld", + (long)(datalen - data_head), + channelp->remote.window_size, + (long)channelp->read_avail)); session->packAdd_channelp = channelp; /* Adjust the window based on the block we freed */ -libssh2_packet_add_jump_point1: - session->packAdd_state = libssh2_NB_state_jump1; - rc = _libssh2_channel_receive_window_adjust(session-> - packAdd_channelp, - (uint32_t)(datalen - 13), - 1, NULL); +ssh2_packet_add_jump_point1: + session->packAdd_state = ssh2_NB_state_jump1; + rc = ssh2_channel_receive_window_adjust(session-> + packAdd_channelp, + (uint32_t)(datalen - 13), + 1, NULL); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; - session->packAdd_state = libssh2_NB_state_idle; + session->packAdd_state = ssh2_NB_state_idle; return 0; } @@ -1050,9 +1032,9 @@ libssh2_packet_add_jump_point1: * Spec says we MAY ignore bytes sent beyond * packet_size */ - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED, - "Packet contains more data than we offered" - " to receive, truncating"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED, + "Packet contains more data than we offered" + " to receive, truncating"); datalen = channelp->remote.packet_size + data_head; } if(channelp->remote.window_size <= channelp->read_avail) { @@ -1060,11 +1042,10 @@ libssh2_packet_add_jump_point1: * Spec says we MAY ignore bytes sent beyond * window_size */ - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED, - "The current receive window is full," - " data ignored"); - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED, + "The current receive window is full, data ignored"); + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; } /* Reset EOF status */ @@ -1072,9 +1053,9 @@ libssh2_packet_add_jump_point1: if(channelp->read_avail + datalen - data_head > channelp->remote.window_size) { - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED, - "Remote sent more data than current " - "window allows, truncating"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED, + "Remote sent more data than current " + "window allows, truncating"); datalen = channelp->remote.window_size - channelp->read_avail + data_head; } @@ -1084,11 +1065,11 @@ libssh2_packet_add_jump_point1: * from an upper layer */ channelp->read_avail += datalen - data_head; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "increasing read_avail by %ld bytes to %ld/%u", - (long)(datalen - data_head), - (long)channelp->read_avail, - channelp->remote.window_size)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "increasing read_avail by %ld bytes to %ld/%u", + (long)(datalen - data_head), + (long)channelp->read_avail, + channelp->remote.window_size)); break; @@ -1100,20 +1081,18 @@ libssh2_packet_add_jump_point1: case SSH_MSG_CHANNEL_EOF: if(datalen >= 5) channelp = - _libssh2_channel_locate(session, - _libssh2_ntohu32(data + 1)); + ssh2_channel_locate(session, ssh2_ntohu32(data + 1)); if(!channelp) /* We may have freed already, quietly ignore this... */ ; else { - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "EOF received for channel %u/%u", - channelp->local.id, - channelp->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "EOF received for channel %u/%u", + channelp->local.id, channelp->remote.id)); channelp->remote.eof = 1; } - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; /* @@ -1135,30 +1114,29 @@ libssh2_packet_add_jump_point1: buf.dataptr++; /* Advance past packet type */ - if(_libssh2_get_u32(&buf, &channel)) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected channel value."); + if(ssh2_get_u32(&buf, &channel)) { + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected channel value."); goto clean_exit; } - if(_libssh2_get_string(&buf, &request, &r_len)) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected request value."); + if(ssh2_get_string(&buf, &request, &r_len)) { + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected request value."); goto clean_exit; } len = (uint32_t)r_len; - if(_libssh2_get_byte(&buf, &want_reply)) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected want reply value."); + if(ssh2_get_byte(&buf, &want_reply)) { + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected want reply value."); goto clean_exit; } - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Channel %u received request type " - "%.*s (wr %X)", - channel, (int)len, request, want_reply)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Channel %u received request type %.*s (wr %X)", + channel, (int)len, request, want_reply)); if(len == strlen("exit-status") && !memcmp("exit-status", request, strlen("exit-status"))) { @@ -1166,24 +1144,23 @@ libssh2_packet_add_jump_point1: /* we have got "exit-status" packet. Set the session value. */ if(datalen >= 20) - channelp = _libssh2_channel_locate(session, channel); + channelp = ssh2_channel_locate(session, channel); if(channelp) { uint32_t status = 0; - if(_libssh2_get_u32(&buf, &status)) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "exit-signal status error"); + if(ssh2_get_u32(&buf, &status)) { + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "exit-signal status error"); } channelp->exit_status = (int)status; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Exit status %d received for " - "channel %u/%u", - channelp->exit_status, - channelp->local.id, - channelp->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Exit status %d received for channel %u/%u", + channelp->exit_status, + channelp->local.id, + channelp->remote.id)); } } else if(len == strlen("exit-signal") && @@ -1192,42 +1169,41 @@ libssh2_packet_add_jump_point1: /* command terminated due to signal */ if(datalen >= 20) - channelp = _libssh2_channel_locate(session, channel); + channelp = ssh2_channel_locate(session, channel); if(channelp) { /* signal name (without SIG prefix) */ unsigned char *sig_name = NULL; size_t sig_len = 0; - if(_libssh2_get_string(&buf, &sig_name, &sig_len)) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "signal name protocol error"); + if(ssh2_get_string(&buf, &sig_name, &sig_len)) { + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "signal name protocol error"); } if(sig_len > UINT32_MAX - 1) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "signal name out of bounds"); + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "signal name out of bounds"); } else if(sig_len > 0) { channelp->exit_signal = - LIBSSH2_ALLOC(session, sig_len + 1); + SSH2_ALLOC(session, sig_len + 1); if(channelp->exit_signal) { memcpy(channelp->exit_signal, sig_name, sig_len); channelp->exit_signal[sig_len] = '\0'; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Exit signal %s received for " - "channel %u/%u", - channelp->exit_signal, - channelp->local.id, - channelp->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Exit signal %s received for " + "channel %u/%u", + channelp->exit_signal, + channelp->local.id, + channelp->remote.id)); } else { - rc = _libssh2_error(session, - LIBSSH2_ERROR_ALLOC, - "exit signal alloc error"); + rc = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "exit signal alloc error"); } } else { @@ -1238,11 +1214,11 @@ libssh2_packet_add_jump_point1: if(want_reply) { unsigned char packet[5]; -libssh2_packet_add_jump_point4: - session->packAdd_state = libssh2_NB_state_jump4; +ssh2_packet_add_jump_point4: + session->packAdd_state = ssh2_NB_state_jump4; packet[0] = SSH_MSG_CHANNEL_FAILURE; memcpy(&packet[1], data + 1, 4); - rc = _libssh2_transport_send(session, packet, 5, NULL, 0); + rc = ssh2_transport_send(session, packet, 5, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; } @@ -1250,8 +1226,8 @@ libssh2_packet_add_jump_point4: clean_exit: - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return rc; /* @@ -1262,24 +1238,22 @@ clean_exit: case SSH_MSG_CHANNEL_CLOSE: if(datalen >= 5) channelp = - _libssh2_channel_locate(session, - _libssh2_ntohu32(data + 1)); + ssh2_channel_locate(session, ssh2_ntohu32(data + 1)); if(!channelp) { /* We may have freed already, quietly ignore this... */ - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; } - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Close received for channel %u/%u", - channelp->local.id, - channelp->remote.id)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Close received for channel %u/%u", + channelp->local.id, channelp->remote.id)); channelp->remote.close = 1; channelp->remote.eof = 1; - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return 0; /* @@ -1295,7 +1269,7 @@ clean_exit: ; else if((datalen >= (strlen("forwarded-tcpip") + 5)) && (strlen("forwarded-tcpip") == - _libssh2_ntohu32(data + 1)) && + ssh2_ntohu32(data + 1)) && (memcmp(data + 5, "forwarded-tcpip", strlen("forwarded-tcpip")) == 0)) { @@ -1303,27 +1277,27 @@ clean_exit: memset(&session->packAdd_Qlstn_state, 0, sizeof(session->packAdd_Qlstn_state)); -libssh2_packet_add_jump_point2: - session->packAdd_state = libssh2_NB_state_jump2; +ssh2_packet_add_jump_point2: + session->packAdd_state = ssh2_NB_state_jump2; rc = packet_queue_listener(session, data, datalen, &session->packAdd_Qlstn_state); } else if((datalen >= (strlen("x11") + 5)) && - ((strlen("x11")) == _libssh2_ntohu32(data + 1)) && + ((strlen("x11")) == ssh2_ntohu32(data + 1)) && (memcmp(data + 5, "x11", strlen("x11")) == 0)) { /* init the state struct */ memset(&session->packAdd_x11open_state, 0, sizeof(session->packAdd_x11open_state)); -libssh2_packet_add_jump_point3: - session->packAdd_state = libssh2_NB_state_jump3; +ssh2_packet_add_jump_point3: + session->packAdd_state = ssh2_NB_state_jump3; rc = packet_x11_open(session, data, datalen, &session->packAdd_x11open_state); } else if((datalen >= (strlen("auth-agent@openssh.com") + 5)) && (strlen("auth-agent@openssh.com") == - _libssh2_ntohu32(data + 1)) && + ssh2_ntohu32(data + 1)) && (memcmp(data + 5, "auth-agent@openssh.com", strlen("auth-agent@openssh.com")) == 0)) { @@ -1331,16 +1305,16 @@ libssh2_packet_add_jump_point3: memset(&session->packAdd_authagent_state, 0, sizeof(session->packAdd_authagent_state)); -libssh2_packet_add_jump_authagent: - session->packAdd_state = libssh2_NB_state_jumpauthagent; +ssh2_packet_add_jump_authagent: + session->packAdd_state = ssh2_NB_state_jumpauthagent; rc = packet_authagent_open(session, data, datalen, &session->packAdd_authagent_state); } if(rc == LIBSSH2_ERROR_EAGAIN) return rc; - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return rc; /* @@ -1352,84 +1326,80 @@ libssh2_packet_add_jump_authagent: if(datalen < 9) ; else { - uint32_t bytestoadd = _libssh2_ntohu32(data + 5); + uint32_t bytestoadd = ssh2_ntohu32(data + 5); channelp = - _libssh2_channel_locate(session, - _libssh2_ntohu32(data + 1)); + ssh2_channel_locate(session, ssh2_ntohu32(data + 1)); if(channelp) { if(bytestoadd > UINT32_MAX - channelp->local.window_size) { - rc = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Window adjust out of bounds"); + rc = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Window adjust out of bounds"); } else { channelp->local.window_size += bytestoadd; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Window adjust for channel %u/%u, " - "adding %u bytes, new window_size=%u", - channelp->local.id, - channelp->remote.id, - bytestoadd, - channelp->local.window_size)); + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Window adjust for channel %u/%u, " + "adding %u bytes, new window_size=%u", + channelp->local.id, + channelp->remote.id, + bytestoadd, + channelp->local.window_size)); } } } - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return rc; default: break; } - session->packAdd_state = libssh2_NB_state_sent; + session->packAdd_state = ssh2_NB_state_sent; } - if(session->packAdd_state == libssh2_NB_state_sent) { - struct packet *packetp = - LIBSSH2_ALLOC(session, sizeof(struct packet)); + if(session->packAdd_state == ssh2_NB_state_sent) { + struct packet *packetp = SSH2_ALLOC(session, sizeof(struct packet)); if(!packetp) { - _libssh2_debug((session, LIBSSH2_ERROR_ALLOC, - "memory for packet")); - LIBSSH2_FREE(session, data); - session->packAdd_state = libssh2_NB_state_idle; + ssh2_deb((session, LIBSSH2_ERROR_ALLOC, "memory for packet")); + SSH2_FREE(session, data); + session->packAdd_state = ssh2_NB_state_idle; return LIBSSH2_ERROR_ALLOC; } packetp->data = data; packetp->data_len = datalen; packetp->data_head = data_head; - _libssh2_list_add(&session->packets, &packetp->node); + ssh2_list_add(&session->packets, &packetp->node); - session->packAdd_state = libssh2_NB_state_sent1; + session->packAdd_state = ssh2_NB_state_sent1; } if((msg == SSH_MSG_KEXINIT && - !(session->state & LIBSSH2_STATE_EXCHANGING_KEYS)) || - (session->packAdd_state == libssh2_NB_state_sent2)) { + !(session->state & SSH2_STATE_EXCHANGING_KEYS)) || + (session->packAdd_state == ssh2_NB_state_sent2)) { - if(session->packAdd_state == libssh2_NB_state_sent1) { + if(session->packAdd_state == ssh2_NB_state_sent1) { /* * Remote wants new keys. * It is already in the brigade, * let's call back into ourselves */ - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Renegotiating Keys")); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Renegotiating Keys")); - session->packAdd_state = libssh2_NB_state_sent2; + session->packAdd_state = ssh2_NB_state_sent2; } /* * The KEXINIT message has been added to the queue. The packAdd and - * readPack states need to be reset because _libssh2_kex_exchange - * (eventually) calls upon _libssh2_transport_read to read the rest of + * readPack states need to be reset because ssh2_kex_exchange() + * (eventually) calls upon ssh2_transport_read() to read the rest of * the key exchange conversation. */ - session->readPack_state = libssh2_NB_state_idle; + session->readPack_state = ssh2_NB_state_idle; session->packet.total_num = 0; - session->packAdd_state = libssh2_NB_state_idle; - session->fullpacket_state = libssh2_NB_state_idle; + session->packAdd_state = ssh2_NB_state_idle; + session->fullpacket_state = ssh2_NB_state_idle; memset(&session->startup_key_state, 0, sizeof(session->startup_key_state)); @@ -1438,12 +1408,12 @@ libssh2_packet_add_jump_authagent: * If there was a key reexchange failure, let's hope we did not * send NEWKEYS yet, otherwise remote drops us like a rock */ - rc = _libssh2_kex_exchange(session, 1, &session->startup_key_state); + rc = ssh2_kex_exchange(session, 1, &session->startup_key_state); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; } - session->packAdd_state = libssh2_NB_state_idle; + session->packAdd_state = ssh2_NB_state_idle; return 0; } @@ -1451,16 +1421,15 @@ libssh2_packet_add_jump_authagent: * Scan the brigade for a matching packet type, optionally poll the socket for * a packet first */ -int _libssh2_packet_ask(LIBSSH2_SESSION *session, unsigned char packet_type, - unsigned char **data, size_t *data_len, - int match_ofs, const unsigned char *match_buf, - size_t match_len) +int ssh2_packet_ask(LIBSSH2_SESSION *session, unsigned char packet_type, + unsigned char **data, size_t *data_len, + int match_ofs, const unsigned char *match_buf, + size_t match_len) { - struct packet *packet = _libssh2_list_first(&session->packets); + struct packet *packet = ssh2_list_first(&session->packets); - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Looking for packet of type: %u", - (unsigned int)packet_type)); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Looking for packet of type: %u", + (unsigned int)packet_type)); while(packet) { if(packet->data[0] == packet_type && @@ -1471,22 +1440,21 @@ int _libssh2_packet_ask(LIBSSH2_SESSION *session, unsigned char packet_type, *data_len = packet->data_len; /* unlink struct from session->packets */ - _libssh2_list_remove(&packet->node); + ssh2_list_remove(&packet->node); - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); return 0; } else if(session->kex_strict && - (session->state & LIBSSH2_STATE_INITIAL_KEX)) { + (session->state & SSH2_STATE_INITIAL_KEX)) { libssh2_session_disconnect(session, "strict KEX violation: " "unexpected packet type"); - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, - "strict KEX violation: " - "unexpected packet type"); + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_DISCONNECT, + "strict KEX violation: unexpected packet type"); } - packet = _libssh2_list_next(&packet->node); + packet = ssh2_list_next(&packet->node); } return -1; } @@ -1495,19 +1463,19 @@ int _libssh2_packet_ask(LIBSSH2_SESSION *session, unsigned char packet_type, * Scan for any of a list of packet types in the brigade, optionally poll the * socket for a packet first */ -int _libssh2_packet_askv(LIBSSH2_SESSION *session, - const unsigned char *packet_types, - unsigned char **data, size_t *data_len, - int match_ofs, - const unsigned char *match_buf, - size_t match_len) +int ssh2_packet_askv(LIBSSH2_SESSION *session, + const unsigned char *packet_types, + unsigned char **data, size_t *data_len, + int match_ofs, + const unsigned char *match_buf, + size_t match_len) { size_t i, packet_types_len = strlen((const char *)packet_types); for(i = 0; i < packet_types_len; i++) { - if(_libssh2_packet_ask(session, packet_types[i], data, - data_len, match_ofs, - match_buf, match_len) == 0) { + if(ssh2_packet_ask(session, packet_types[i], data, + data_len, match_ofs, + match_buf, match_len) == 0) { return 0; } } @@ -1516,24 +1484,24 @@ int _libssh2_packet_askv(LIBSSH2_SESSION *session, } /* - * Loops _libssh2_transport_read() until the packet requested is available + * Loops ssh2_transport_read() until the packet requested is available * SSH_DISCONNECT or a SOCKET_DISCONNECTED causes a bailout * * Returns negative on error * Returns 0 when it has taken care of the requested packet. */ -int _libssh2_packet_require(LIBSSH2_SESSION *session, - unsigned char packet_type, - unsigned char **data, size_t *data_len, - int match_ofs, - const unsigned char *match_buf, - size_t match_len, - struct packet_require_state *state) +int ssh2_packet_require(LIBSSH2_SESSION *session, + unsigned char packet_type, + unsigned char **data, size_t *data_len, + int match_ofs, + const unsigned char *match_buf, + size_t match_len, + struct packet_require_state *state) { if(state->start == 0) { - if(_libssh2_packet_ask(session, packet_type, data, data_len, - match_ofs, match_buf, - match_len) == 0) { + if(ssh2_packet_ask(session, packet_type, data, data_len, + match_ofs, match_buf, + match_len) == 0) { /* A packet was available in the packet brigade */ return 0; } @@ -1541,10 +1509,10 @@ int _libssh2_packet_require(LIBSSH2_SESSION *session, state->start = time(NULL); } - while(session->socket_state == LIBSSH2_SOCKET_CONNECTED) { + while(session->socket_state == SSH2_SOCKET_CONNECTED) { int ret; session->fullpacket_required_type = packet_type; - ret = _libssh2_transport_read(session); + ret = ssh2_transport_read(session); session->fullpacket_required_type = 0; if(ret == LIBSSH2_ERROR_EAGAIN) return ret; @@ -1555,8 +1523,8 @@ int _libssh2_packet_require(LIBSSH2_SESSION *session, } else if(ret == packet_type) { /* Be lazy, let packet_ask pull it out of the brigade */ - ret = _libssh2_packet_ask(session, packet_type, data, data_len, - match_ofs, match_buf, match_len); + ret = ssh2_packet_ask(session, packet_type, data, data_len, + match_ofs, match_buf, match_len); state->start = 0; return ret; } @@ -1578,44 +1546,43 @@ int _libssh2_packet_require(LIBSSH2_SESSION *session, } /* - * Loops _libssh2_transport_read() until any packet is available and promptly + * Loops ssh2_transport_read() until any packet is available and promptly * discards it. * Used during KEX exchange to discard badly guessed KEX_INIT packets */ -int _libssh2_packet_burn(LIBSSH2_SESSION *session, - libssh2_nonblocking_states *state) +int ssh2_packet_burn(LIBSSH2_SESSION *session, ssh2_NB_states *state) { unsigned char *data; size_t data_len; unsigned char i, all_packets[255]; int ret; - if(*state == libssh2_NB_state_idle) { + if(*state == ssh2_NB_state_idle) { for(i = 1; i < 255; i++) { all_packets[i - 1] = i; } all_packets[254] = 0; - if(_libssh2_packet_askv(session, all_packets, &data, &data_len, 0, - NULL, 0) == 0) { + if(ssh2_packet_askv(session, all_packets, &data, &data_len, 0, + NULL, 0) == 0) { i = data[0]; /* A packet was available in the packet brigade, burn it */ - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); return i; } - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Blocking until packet becomes available to burn")); - *state = libssh2_NB_state_created; + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "Blocking until packet becomes available to burn")); + *state = ssh2_NB_state_created; } - while(session->socket_state == LIBSSH2_SOCKET_CONNECTED) { - ret = _libssh2_transport_read(session); + while(session->socket_state == SSH2_SOCKET_CONNECTED) { + ret = ssh2_transport_read(session); if(ret == LIBSSH2_ERROR_EAGAIN) { return ret; } else if(ret < 0) { - *state = libssh2_NB_state_idle; + *state = ssh2_NB_state_idle; return ret; } else if(ret == 0) { @@ -1624,11 +1591,11 @@ int _libssh2_packet_burn(LIBSSH2_SESSION *session, } /* Be lazy, let packet_ask pull it out of the brigade */ - if(_libssh2_packet_ask(session, (unsigned char)ret, &data, &data_len, - 0, NULL, 0) == 0) { + if(ssh2_packet_ask(session, (unsigned char)ret, &data, &data_len, + 0, NULL, 0) == 0) { /* Smoke 'em if you got 'em */ - LIBSSH2_FREE(session, data); - *state = libssh2_NB_state_idle; + SSH2_FREE(session, data); + *state = ssh2_NB_state_idle; return ret; } } @@ -1638,19 +1605,19 @@ int _libssh2_packet_burn(LIBSSH2_SESSION *session, } /* - * Loops _libssh2_transport_read() until one of a list of packet types + * Loops ssh2_transport_read() until one of a list of packet types * requested is available. SSH_DISCONNECT or a SOCKET_DISCONNECTED causes * a bailout. packet_types is a null-terminated list of packet_type numbers */ -int _libssh2_packet_requirev(LIBSSH2_SESSION *session, - const unsigned char *packet_types, - unsigned char **data, size_t *data_len, - int match_ofs, - const unsigned char *match_buf, size_t match_len, - struct packet_requirev_state *state) +int ssh2_packet_requirev(LIBSSH2_SESSION *session, + const unsigned char *packet_types, + unsigned char **data, size_t *data_len, + int match_ofs, + const unsigned char *match_buf, size_t match_len, + struct packet_requirev_state *state) { - if(_libssh2_packet_askv(session, packet_types, data, data_len, match_ofs, - match_buf, match_len) == 0) { + if(ssh2_packet_askv(session, packet_types, data, data_len, match_ofs, + match_buf, match_len) == 0) { /* One of the packets listed was available in the packet brigade */ state->start = 0; return 0; @@ -1660,8 +1627,8 @@ int _libssh2_packet_requirev(LIBSSH2_SESSION *session, state->start = time(NULL); } - while(session->socket_state != LIBSSH2_SOCKET_DISCONNECTED) { - int ret = _libssh2_transport_read(session); + while(session->socket_state != SSH2_SOCKET_DISCONNECTED) { + int ret = ssh2_transport_read(session); if((ret < 0) && (ret != LIBSSH2_ERROR_EAGAIN)) { state->start = 0; return ret; @@ -1681,9 +1648,9 @@ int _libssh2_packet_requirev(LIBSSH2_SESSION *session, if(strchr((const char *)packet_types, ret)) { /* Be lazy, let packet_ask pull it out of the brigade */ - ret = _libssh2_packet_askv(session, packet_types, data, - data_len, match_ofs, match_buf, - match_len); + ret = ssh2_packet_askv(session, packet_types, data, + data_len, match_ofs, match_buf, + match_len); state->start = 0; return ret; } diff --git a/src/packet.h b/src/packet.h index e1246005..343ebced 100644 --- a/src/packet.h +++ b/src/packet.h @@ -40,38 +40,37 @@ * SPDX-License-Identifier: BSD-3-Clause */ -int _libssh2_packet_read(LIBSSH2_SESSION *session); +int ssh2_packet_read(LIBSSH2_SESSION *session); -int _libssh2_packet_ask(LIBSSH2_SESSION *session, unsigned char packet_type, - unsigned char **data, size_t *data_len, - int match_ofs, +int ssh2_packet_ask(LIBSSH2_SESSION *session, unsigned char packet_type, + unsigned char **data, size_t *data_len, + int match_ofs, + const unsigned char *match_buf, + size_t match_len); + +int ssh2_packet_askv(LIBSSH2_SESSION *session, + const unsigned char *packet_types, + unsigned char **data, size_t *data_len, + int match_ofs, + const unsigned char *match_buf, + size_t match_len); +int ssh2_packet_require(LIBSSH2_SESSION *session, + unsigned char packet_type, unsigned char **data, + size_t *data_len, int match_ofs, const unsigned char *match_buf, - size_t match_len); - -int _libssh2_packet_askv(LIBSSH2_SESSION *session, + size_t match_len, + struct packet_require_state *state); +int ssh2_packet_requirev(LIBSSH2_SESSION *session, const unsigned char *packet_types, unsigned char **data, size_t *data_len, int match_ofs, const unsigned char *match_buf, - size_t match_len); -int _libssh2_packet_require(LIBSSH2_SESSION *session, - unsigned char packet_type, unsigned char **data, - size_t *data_len, int match_ofs, - const unsigned char *match_buf, - size_t match_len, - struct packet_require_state *state); -int _libssh2_packet_requirev(LIBSSH2_SESSION *session, - const unsigned char *packet_types, - unsigned char **data, size_t *data_len, - int match_ofs, - const unsigned char *match_buf, - size_t match_len, - struct packet_requirev_state *state); -int _libssh2_packet_burn(LIBSSH2_SESSION *session, - libssh2_nonblocking_states *state); -int _libssh2_packet_write(LIBSSH2_SESSION *session, unsigned char *data, - unsigned long data_len); -int _libssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, - size_t datalen, int macstate, uint32_t seq); + size_t match_len, + struct packet_requirev_state *state); +int ssh2_packet_burn(LIBSSH2_SESSION *session, ssh2_NB_states *state); +int ssh2_packet_write(LIBSSH2_SESSION *session, unsigned char *data, + unsigned long data_len); +int ssh2_packet_add(LIBSSH2_SESSION *session, unsigned char *data, + size_t datalen, int macstate, uint32_t seq); #endif /* LIBSSH2_PACKET_H */ diff --git a/src/pem.c b/src/pem.c index 6ef2a794..b918dc7b 100644 --- a/src/pem.c +++ b/src/pem.c @@ -104,11 +104,11 @@ static unsigned char hex_decode(char digit) ((digit >= 'A') ? (0xA + (digit - 'A')) : (digit - '0')); } -int _libssh2_pem_parse(LIBSSH2_SESSION *session, - const char *headerbegin, - const char *headerend, - const unsigned char *passphrase, - FILE *fp, unsigned char **data, size_t *datalen) +int ssh2_pem_parse(LIBSSH2_SESSION *session, + const char *headerbegin, + const char *headerend, + const unsigned char *passphrase, + FILE *fp, unsigned char **data, size_t *datalen) { int ret = -1; char *filedata = NULL; @@ -116,62 +116,61 @@ int _libssh2_pem_parse(LIBSSH2_SESSION *session, size_t filedata_len; if(fseek(fp, 0L, SEEK_END) != 0) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Bad seek to file end in PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Bad seek to file end in PEM parsing"); goto out; } file_size = ftell(fp); if(file_size < 0) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Error determining size in PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Error determining size in PEM parsing"); goto out; } if(file_size == 0) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Zero-length file in PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Zero-length file in PEM parsing"); goto out; } if(file_size > (1024 * 1024)) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Input too large in PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_FILE, + "Input too large in PEM parsing"); goto out; } if(fseek(fp, 0L, SEEK_SET) != 0) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Bad seek to 0 in PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_FILE, "Bad seek to 0 in PEM parsing"); goto out; } filedata_len = (size_t)file_size; - filedata = LIBSSH2_ALLOC(session, filedata_len); + filedata = SSH2_ALLOC(session, filedata_len); if(!filedata) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for PEM parsing"); goto out; } if(fread(filedata, 1, filedata_len, fp) != filedata_len) { - _libssh2_error(session, LIBSSH2_ERROR_FILE, "Bad read in PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_FILE, "Bad read in PEM parsing"); goto out; } - ret = _libssh2_pem_parse_memory(session, headerbegin, headerend, - passphrase, filedata, filedata_len, - data, datalen); + ret = ssh2_pem_parse_memory(session, headerbegin, headerend, + passphrase, filedata, filedata_len, + data, datalen); out: if(filedata) { - LIBSSH2_FREE(session, filedata); + SSH2_FREE(session, filedata); } return ret; } -int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, - const char *headerbegin, - const char *headerend, - const unsigned char *passphrase, - const char *filedata, size_t filedata_len, - unsigned char **data, size_t *datalen) +int ssh2_pem_parse_memory(LIBSSH2_SESSION *session, + const char *headerbegin, + const char *headerend, + const unsigned char *passphrase, + const char *filedata, size_t filedata_len, + unsigned char **data, size_t *datalen) { char line[LINE_SIZE]; unsigned char iv[LINE_SIZE]; @@ -206,7 +205,7 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, goto out; } - all_methods = libssh2_crypt_methods(); + all_methods = ssh2_crypt_methods(); /* !checksrc! disable EQUALSNULL 1 */ while((cur_method = *all_methods++) != NULL) { if(*cur_method->pem_annotation && @@ -220,8 +219,8 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, /* None of the available crypt methods were able to decrypt the key */ if(!method) { - _libssh2_error(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED, - "Unable to decrypt PEM, unsupported algorithm"); + ssh2_err(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED, + "Unable to decrypt PEM, unsupported algorithm"); ret = -1; goto out; } @@ -247,10 +246,10 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, size_t linelen; linelen = strlen(line); - tmp = LIBSSH2_REALLOC(session, b64data, b64datalen + linelen); + tmp = SSH2_REALLOC(session, b64data, b64datalen + linelen); if(!tmp) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for PEM parsing"); ret = -1; goto out; } @@ -271,15 +270,15 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, return -1; } - if(_libssh2_base64_decode(session, (char **)data, datalen, - b64data, b64datalen)) { + if(ssh2_base64_decode(session, (char **)data, datalen, + b64data, b64datalen)) { ret = -1; goto out; } if(*datalen == 0) { /* Invalid decode */ - LIBSSH2_FREE(session, *data); + SSH2_FREE(session, *data); ret = -1; goto out; } @@ -292,26 +291,26 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, int blocksize = method->blocksize; void *abstract; unsigned char secret[2 * MD5_DIGEST_LENGTH]; - libssh2_md5_ctx fingerprint_ctx; + ssh2_md5_ctx fingerprint_ctx; /* Perform key derivation (PBKDF1/MD5) */ - if(!libssh2_md5_init(&fingerprint_ctx) || - !libssh2_md5_update(fingerprint_ctx, passphrase, - strlen((const char *)passphrase)) || - !libssh2_md5_update(fingerprint_ctx, iv, 8) || - !libssh2_md5_final(fingerprint_ctx, secret)) { + if(!ssh2_md5_init(&fingerprint_ctx) || + !ssh2_md5_update(fingerprint_ctx, passphrase, + strlen((const char *)passphrase)) || + !ssh2_md5_update(fingerprint_ctx, iv, 8) || + !ssh2_md5_final(fingerprint_ctx, secret)) { ret = -1; goto out; } if(method->secret_len > MD5_DIGEST_LENGTH) { - if(!libssh2_md5_init(&fingerprint_ctx) || - !libssh2_md5_update(fingerprint_ctx, secret, - MD5_DIGEST_LENGTH) || - !libssh2_md5_update(fingerprint_ctx, passphrase, - strlen((const char *)passphrase)) || - !libssh2_md5_update(fingerprint_ctx, iv, 8) || - !libssh2_md5_final(fingerprint_ctx, - secret + MD5_DIGEST_LENGTH)) { + if(!ssh2_md5_init(&fingerprint_ctx) || + !ssh2_md5_update(fingerprint_ctx, secret, + MD5_DIGEST_LENGTH) || + !ssh2_md5_update(fingerprint_ctx, passphrase, + strlen((const char *)passphrase)) || + !ssh2_md5_update(fingerprint_ctx, iv, 8) || + !ssh2_md5_final(fingerprint_ctx, + secret + MD5_DIGEST_LENGTH)) { ret = -1; goto out; } @@ -320,34 +319,34 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, /* Initialize the decryption */ if(method->init(session, method, iv, &free_iv, secret, &free_secret, 0, &abstract)) { - _libssh2_explicit_zero((char *)secret, sizeof(secret)); - _libssh2_explicit_zero(*data, *datalen); - LIBSSH2_FREE(session, *data); + ssh2_explicit_zero((char *)secret, sizeof(secret)); + ssh2_explicit_zero(*data, *datalen); + SSH2_FREE(session, *data); ret = -1; goto out; } if(free_secret) { - _libssh2_explicit_zero((char *)secret, sizeof(secret)); + ssh2_explicit_zero((char *)secret, sizeof(secret)); } /* Do the actual decryption */ if((*datalen % blocksize) != 0) { - _libssh2_explicit_zero((char *)secret, sizeof(secret)); + ssh2_explicit_zero((char *)secret, sizeof(secret)); method->dtor(session, &abstract); - _libssh2_explicit_zero(*data, *datalen); - LIBSSH2_FREE(session, *data); + ssh2_explicit_zero(*data, *datalen); + SSH2_FREE(session, *data); ret = -1; goto out; } - if(method->flags & LIBSSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET) { + if(method->flags & SSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET) { if(method->crypt(session, 0, *data, *datalen, &abstract, 0)) { ret = LIBSSH2_ERROR_DECRYPT; - _libssh2_explicit_zero((char *)secret, sizeof(secret)); + ssh2_explicit_zero((char *)secret, sizeof(secret)); method->dtor(session, &abstract); - _libssh2_explicit_zero(*data, *datalen); - LIBSSH2_FREE(session, *data); + ssh2_explicit_zero(*data, *datalen); + SSH2_FREE(session, *data); goto out; } } @@ -362,10 +361,10 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, ? LAST_BLOCK : MIDDLE_BLOCK))) { ret = LIBSSH2_ERROR_DECRYPT; - _libssh2_explicit_zero((char *)secret, sizeof(secret)); + ssh2_explicit_zero((char *)secret, sizeof(secret)); method->dtor(session, &abstract); - _libssh2_explicit_zero(*data, *datalen); - LIBSSH2_FREE(session, *data); + ssh2_explicit_zero(*data, *datalen); + SSH2_FREE(session, *data); goto out; } @@ -378,19 +377,19 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, if(padding > *datalen) { /* Invalid padding len */ ret = LIBSSH2_ERROR_DECRYPT; - _libssh2_explicit_zero(*data, *datalen); - LIBSSH2_FREE(session, *data); + ssh2_explicit_zero(*data, *datalen); + SSH2_FREE(session, *data); goto out; } memset(&(*data)[*datalen - padding], 0, padding); *datalen -= padding; /* Clean up */ - _libssh2_explicit_zero((char *)secret, sizeof(secret)); + ssh2_explicit_zero((char *)secret, sizeof(secret)); method->dtor(session, &abstract); #else - _libssh2_error(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED, - "Unable to decrypt PEM, MD5 not enabled"); + ssh2_err(session, LIBSSH2_ERROR_ALGO_UNSUPPORTED, + "Unable to decrypt PEM, MD5 not enabled"); ret = -1; goto out; #endif @@ -399,8 +398,8 @@ int _libssh2_pem_parse_memory(LIBSSH2_SESSION *session, ret = 0; out: if(b64data) { - _libssh2_explicit_zero(b64data, b64datalen); - LIBSSH2_FREE(session, b64data); + ssh2_explicit_zero(b64data, b64datalen); + SSH2_FREE(session, b64data); } return ret; } @@ -410,11 +409,11 @@ out: #define OPENSSH_HEADER_BEGIN "-----BEGIN OPENSSH PRIVATE KEY-----" #define OPENSSH_HEADER_END "-----END OPENSSH PRIVATE KEY-----" -static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, - const unsigned char *passphrase, - const char *b64data, - size_t b64datalen, - struct string_buf **decrypted_buf) +static int ssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, + const unsigned char *passphrase, + const char *b64data, + size_t b64datalen, + struct string_buf **decrypted_buf) { const struct crypt_method *method = NULL; struct string_buf decoded, decrypted, kdf_buf; @@ -437,8 +436,7 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, *decrypted_buf = NULL; /* decode file */ - if(_libssh2_base64_decode(session, (char **)&f, &f_len, - b64data, b64datalen)) { + if(ssh2_base64_decode(session, (char **)&f, &f_len, b64data, b64datalen)) { ret = -1; goto out; } @@ -449,35 +447,31 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, decoded.len = f_len; if(decoded.len < strlen(AUTH_MAGIC)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, "key too short"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "key too short"); goto out; } if(strncmp((const char *)decoded.dataptr, AUTH_MAGIC, strlen(AUTH_MAGIC)) != 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "key auth magic mismatch"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "key auth magic mismatch"); goto out; } decoded.dataptr += strlen(AUTH_MAGIC) + 1; - if(_libssh2_get_string(&decoded, &ciphername, &tmp_len) || - tmp_len == 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "ciphername is missing"); + if(ssh2_get_string(&decoded, &ciphername, &tmp_len) || tmp_len == 0) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "ciphername is missing"); goto out; } - if(_libssh2_get_string(&decoded, &kdfname, &tmp_len) || - tmp_len == 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "kdfname is missing"); + if(ssh2_get_string(&decoded, &kdfname, &tmp_len) || tmp_len == 0) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "kdfname is missing"); goto out; } - if(_libssh2_get_string(&decoded, &kdf, &kdf_len)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, "kdf is missing"); + if(ssh2_get_string(&decoded, &kdf, &kdf_len)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "kdf is missing"); goto out; } else { @@ -495,34 +489,33 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, if(strcmp((const char *)kdfname, "none") != 0 && strcmp((const char *)kdfname, "bcrypt") != 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, "unknown cipher"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "unknown cipher"); goto out; } if(!strcmp((const char *)kdfname, "none") && strcmp((const char *)ciphername, "none") != 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, "invalid format"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, "invalid format"); goto out; } - if(_libssh2_get_u32(&decoded, &nkeys) != 0 || nkeys != 1) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Multiple keys are unsupported"); + if(ssh2_get_u32(&decoded, &nkeys) != 0 || nkeys != 1) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Multiple keys are unsupported"); goto out; } /* unencrypted public key */ - if(_libssh2_get_string(&decoded, &buf, &tmp_len) || tmp_len == 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid private key; " - "expect embedded public key"); + if(ssh2_get_string(&decoded, &buf, &tmp_len) || tmp_len == 0) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Invalid private key; expect embedded public key"); goto out; } - if(_libssh2_get_string(&decoded, &buf, &tmp_len) || tmp_len == 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Private key data not found"); + if(ssh2_get_string(&decoded, &buf, &tmp_len) || tmp_len == 0) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Private key data not found"); goto out; } @@ -533,21 +526,20 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, if(ciphername && strcmp((const char *)ciphername, "none") != 0) { const struct crypt_method **all_methods, *cur_method; - all_methods = libssh2_crypt_methods(); + all_methods = ssh2_crypt_methods(); /* !checksrc! disable EQUALSNULL 1 */ while((cur_method = *all_methods++) != NULL) { - if(*cur_method->name && - memcmp(ciphername, cur_method->name, - strlen(cur_method->name)) == 0) { - method = cur_method; - } + if(*cur_method->name && memcmp(ciphername, cur_method->name, + strlen(cur_method->name)) == 0) { + method = cur_method; + } } /* None of the available crypt methods were able to decrypt the key */ if(!method) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "No supported cipher found"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "No supported cipher found"); goto out; } } @@ -561,50 +553,50 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, ivlen = method->iv_len; total_len = keylen + ivlen; - key = LIBSSH2_CALLOC(session, total_len); + key = SSH2_CALLOC(session, total_len); if(!key) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Could not alloc key"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Could not alloc key"); goto out; } if(strcmp((const char *)kdfname, "bcrypt") == 0 && passphrase) { - if((_libssh2_get_string(&kdf_buf, &salt, &salt_len)) || - (_libssh2_get_u32(&kdf_buf, &rounds) != 0)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "kdf contains unexpected values"); + if((ssh2_get_string(&kdf_buf, &salt, &salt_len)) || + (ssh2_get_u32(&kdf_buf, &rounds) != 0)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "kdf contains unexpected values"); goto out; } - if(_libssh2_bcrypt_pbkdf((const char *)passphrase, - strlen((const char *)passphrase), - salt, salt_len, key, - keylen + ivlen, rounds) < 0) { - ret = _libssh2_error(session, LIBSSH2_ERROR_DECRYPT, - "invalid format"); + if(ssh2_bcrypt_pbkdf((const char *)passphrase, + strlen((const char *)passphrase), + salt, salt_len, key, + keylen + ivlen, rounds) < 0) { + ret = ssh2_err(session, LIBSSH2_ERROR_DECRYPT, + "invalid format"); goto out; } } else { - ret = _libssh2_error(session, LIBSSH2_ERROR_KEYFILE_AUTH_FAILED, - "bcrypted without passphrase"); + ret = ssh2_err(session, LIBSSH2_ERROR_KEYFILE_AUTH_FAILED, + "bcrypted without passphrase"); goto out; } /* Set up decryption */ blocksize = method->blocksize; - key_part = LIBSSH2_CALLOC(session, keylen); + key_part = SSH2_CALLOC(session, keylen); if(!key_part) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Could not alloc key part"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Could not alloc key part"); goto out; } - iv_part = LIBSSH2_CALLOC(session, ivlen); + iv_part = SSH2_CALLOC(session, ivlen); if(!iv_part) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Could not alloc iv part"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Could not alloc iv part"); goto out; } @@ -625,7 +617,7 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, goto out; } - if(method->flags & LIBSSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET) { + if(method->flags & SSH2_CRYPT_FLAG_REQUIRES_FULL_PACKET) { if(method->crypt(session, 0, decrypted.data, decrypted.len, &abstract, @@ -662,16 +654,16 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, * appended to the encrypted key */ if(strcmp(method->name, "aes256-gcm@openssh.com") == 0 || strcmp(method->name, "aes128-gcm@openssh.com") == 0) { - if(!_libssh2_check_length(&decoded, 16)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "GCM auth tag missing"); + if(!ssh2_check_length(&decoded, 16)) { + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "GCM auth tag missing"); method->dtor(session, &abstract); goto out; } if(method->crypt(session, 0, decoded.dataptr, 16, &abstract, LAST_BLOCK)) { - ret = _libssh2_error(session, LIBSSH2_ERROR_DECRYPT, - "GCM auth tag invalid"); + ret = ssh2_err(session, LIBSSH2_ERROR_DECRYPT, + "GCM auth tag invalid"); method->dtor(session, &abstract); goto out; } @@ -684,31 +676,29 @@ static int _libssh2_openssh_pem_parse_data(LIBSSH2_SESSION *session, /* Check random bytes match */ - if(_libssh2_get_u32(&decrypted, &check1) != 0 || - _libssh2_get_u32(&decrypted, &check2) != 0 || + if(ssh2_get_u32(&decrypted, &check1) != 0 || + ssh2_get_u32(&decrypted, &check2) != 0 || check1 != check2) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Private key unpack failed (correct password?)"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Private key unpack failed (correct password?)"); ret = LIBSSH2_ERROR_KEYFILE_AUTH_FAILED; goto out; } if(decrypted_buf) { /* copy data to out-going buffer */ - struct string_buf *out_buf = _libssh2_string_buf_new(session); + struct string_buf *out_buf = ssh2_string_buf_new(session); if(!out_buf) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "decrypted struct"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for decrypted struct"); goto out; } - out_buf->data = LIBSSH2_CALLOC(session, decrypted.len); + out_buf->data = SSH2_CALLOC(session, decrypted.len); if(!out_buf->data) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "decrypted struct"); - _libssh2_string_buf_free(session, out_buf); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for decrypted struct"); + ssh2_string_buf_free(session, out_buf); goto out; } memcpy(out_buf->data, decrypted.data, decrypted.len); @@ -723,28 +713,28 @@ out: /* Clean up */ if(key) { - _libssh2_explicit_zero(key, total_len); - LIBSSH2_FREE(session, key); + ssh2_explicit_zero(key, total_len); + SSH2_FREE(session, key); } if(key_part) { - _libssh2_explicit_zero(key_part, keylen); - LIBSSH2_FREE(session, key_part); + ssh2_explicit_zero(key_part, keylen); + SSH2_FREE(session, key_part); } if(iv_part) { - _libssh2_explicit_zero(iv_part, ivlen); - LIBSSH2_FREE(session, iv_part); + ssh2_explicit_zero(iv_part, ivlen); + SSH2_FREE(session, iv_part); } if(f) { - _libssh2_explicit_zero(f, f_len); - LIBSSH2_FREE(session, f); + ssh2_explicit_zero(f, f_len); + SSH2_FREE(session, f); } return ret; } -int _libssh2_openssh_pem_parse(LIBSSH2_SESSION *session, - const unsigned char *passphrase, - FILE *fp, struct string_buf **decrypted_buf) +int ssh2_openssh_pem_parse(LIBSSH2_SESSION *session, + const unsigned char *passphrase, + FILE *fp, struct string_buf **decrypted_buf) { char line[LINE_SIZE]; char *b64data = NULL; @@ -771,10 +761,10 @@ int _libssh2_openssh_pem_parse(LIBSSH2_SESSION *session, size_t linelen; linelen = strlen(line); - tmp = LIBSSH2_REALLOC(session, b64data, b64datalen + linelen); + tmp = SSH2_REALLOC(session, b64data, b64datalen + linelen); if(!tmp) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for PEM parsing"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for PEM parsing"); ret = -1; goto out; } @@ -795,15 +785,15 @@ int _libssh2_openssh_pem_parse(LIBSSH2_SESSION *session, return -1; } - ret = _libssh2_openssh_pem_parse_data(session, - passphrase, - (const char *)b64data, - b64datalen, - decrypted_buf); + ret = ssh2_openssh_pem_parse_data(session, + passphrase, + (const char *)b64data, + b64datalen, + decrypted_buf); if(b64data) { - _libssh2_explicit_zero(b64data, b64datalen); - LIBSSH2_FREE(session, b64data); + ssh2_explicit_zero(b64data, b64datalen); + SSH2_FREE(session, b64data); } out: @@ -811,11 +801,11 @@ out: return ret; } -int _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, - const unsigned char *passphrase, - const char *filedata, - size_t filedata_len, - struct string_buf **decrypted_buf) +int ssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, + const unsigned char *passphrase, + const char *filedata, + size_t filedata_len, + struct string_buf **decrypted_buf) { char line[LINE_SIZE]; char *b64data = NULL; @@ -824,17 +814,16 @@ int _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, int ret; if(!filedata || filedata_len <= 0) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Error parsing PEM: filedata missing"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Error parsing PEM: filedata missing"); do { *line = '\0'; if(off >= filedata_len) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Error parsing PEM: " - "OpenSSH header not found"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Error parsing PEM: OpenSSH header not found"); if(readline_memory(line, LINE_SIZE, filedata, filedata_len, &off)) { return -1; @@ -849,11 +838,10 @@ int _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, size_t linelen; linelen = strlen(line); - tmp = LIBSSH2_REALLOC(session, b64data, b64datalen + linelen); + tmp = SSH2_REALLOC(session, b64data, b64datalen + linelen); if(!tmp) { - ret = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "PEM parsing"); + ret = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for PEM parsing"); goto out; } memcpy(tmp + b64datalen, line, linelen); @@ -864,8 +852,8 @@ int _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, *line = '\0'; if(off >= filedata_len) { - ret = _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Error parsing PEM: offset out of bounds"); + ret = ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Error parsing PEM: offset out of bounds"); goto out; } @@ -876,16 +864,16 @@ int _libssh2_openssh_pem_parse_memory(LIBSSH2_SESSION *session, } while(strcmp(line, OPENSSH_HEADER_END) != 0); if(!b64data) - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Error parsing PEM: base 64 data missing"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Error parsing PEM: base 64 data missing"); - ret = _libssh2_openssh_pem_parse_data(session, passphrase, b64data, - b64datalen, decrypted_buf); + ret = ssh2_openssh_pem_parse_data(session, passphrase, b64data, + b64datalen, decrypted_buf); out: if(b64data) { - _libssh2_explicit_zero(b64data, b64datalen); - LIBSSH2_FREE(session, b64data); + ssh2_explicit_zero(b64data, b64datalen); + SSH2_FREE(session, b64data); } return ret; } @@ -924,7 +912,7 @@ static int read_asn1_length(const unsigned char *data, return nextpos; } -int _libssh2_pem_decode_sequence(unsigned char **data, size_t *datalen) +int ssh2_pem_decode_sequence(unsigned char **data, size_t *datalen) { size_t len; int lenlen; @@ -951,8 +939,8 @@ int _libssh2_pem_decode_sequence(unsigned char **data, size_t *datalen) return 0; } -int _libssh2_pem_decode_integer(unsigned char **data, size_t *datalen, - unsigned char **i, unsigned int *ilen) +int ssh2_pem_decode_integer(unsigned char **data, size_t *datalen, + unsigned char **i, unsigned int *ilen) { size_t len; int lenlen; diff --git a/src/publickey.c b/src/publickey.c index 58ef5a2f..257a2eb1 100644 --- a/src/publickey.c +++ b/src/publickey.c @@ -43,12 +43,12 @@ #include "channel.h" #include "session.h" -#define LIBSSH2_PUBLICKEY_VERSION 2 +#define SSH2_PUBLICKEY_VERSION 2 /* Numericised response codes -- Not IETF, but local representation */ -#define LIBSSH2_PUBLICKEY_RESPONSE_STATUS 0 -#define LIBSSH2_PUBLICKEY_RESPONSE_VERSION 1 -#define LIBSSH2_PUBLICKEY_RESPONSE_PUBLICKEY 2 +#define SSH2_PUBLICKEY_RESPONSE_STATUS 0 +#define SSH2_PUBLICKEY_RESPONSE_VERSION 1 +#define SSH2_PUBLICKEY_RESPONSE_PUBLICKEY 2 struct publickey_code_list { const char *name; @@ -59,36 +59,36 @@ struct publickey_code_list { #define STRLEN(s) s, sizeof(s) - 1 static const struct publickey_code_list publickey_response_codes[] = { - { STRLEN("status"), LIBSSH2_PUBLICKEY_RESPONSE_STATUS }, - { STRLEN("version"), LIBSSH2_PUBLICKEY_RESPONSE_VERSION }, - { STRLEN("publickey"), LIBSSH2_PUBLICKEY_RESPONSE_PUBLICKEY }, + { STRLEN("status"), SSH2_PUBLICKEY_RESPONSE_STATUS }, + { STRLEN("version"), SSH2_PUBLICKEY_RESPONSE_VERSION }, + { STRLEN("publickey"), SSH2_PUBLICKEY_RESPONSE_PUBLICKEY }, { NULL, 0, 0 } }; /* PUBLICKEY status codes -- IETF defined */ -#define LIBSSH2_PUBLICKEY_SUCCESS 0 -#define LIBSSH2_PUBLICKEY_ACCESS_DENIED 1 -#define LIBSSH2_PUBLICKEY_STORAGE_EXCEEDED 2 -#define LIBSSH2_PUBLICKEY_VERSION_NOT_SUPPORTED 3 -#define LIBSSH2_PUBLICKEY_KEY_NOT_FOUND 4 -#define LIBSSH2_PUBLICKEY_KEY_NOT_SUPPORTED 5 -#define LIBSSH2_PUBLICKEY_KEY_ALREADY_PRESENT 6 -#define LIBSSH2_PUBLICKEY_GENERAL_FAILURE 7 -#define LIBSSH2_PUBLICKEY_REQUEST_NOT_SUPPORTED 8 +#define SSH2_PUBLICKEY_SUCCESS 0 +#define SSH2_PUBLICKEY_ACCESS_DENIED 1 +#define SSH2_PUBLICKEY_STORAGE_EXCEEDED 2 +#define SSH2_PUBLICKEY_VERSION_NOT_SUPPORTED 3 +#define SSH2_PUBLICKEY_KEY_NOT_FOUND 4 +#define SSH2_PUBLICKEY_KEY_NOT_SUPPORTED 5 +#define SSH2_PUBLICKEY_KEY_ALREADY_PRESENT 6 +#define SSH2_PUBLICKEY_GENERAL_FAILURE 7 +#define SSH2_PUBLICKEY_REQUEST_NOT_SUPPORTED 8 -#define LIBSSH2_PUBLICKEY_STATUS_CODE_MAX 8 +#define SSH2_PUBLICKEY_STATUS_CODE_MAX 8 static const struct publickey_code_list publickey_status_codes[] = { - {STRLEN("success"), LIBSSH2_PUBLICKEY_SUCCESS}, - {STRLEN("access denied"), LIBSSH2_PUBLICKEY_ACCESS_DENIED}, - {STRLEN("storage exceeded"), LIBSSH2_PUBLICKEY_STORAGE_EXCEEDED}, - {STRLEN("version not supported"), LIBSSH2_PUBLICKEY_VERSION_NOT_SUPPORTED}, - {STRLEN("key not found"), LIBSSH2_PUBLICKEY_KEY_NOT_FOUND}, - {STRLEN("key not supported"), LIBSSH2_PUBLICKEY_KEY_NOT_SUPPORTED}, - {STRLEN("key already present"), LIBSSH2_PUBLICKEY_KEY_ALREADY_PRESENT}, - {STRLEN("general failure"), LIBSSH2_PUBLICKEY_GENERAL_FAILURE}, - {STRLEN("request not supported"), LIBSSH2_PUBLICKEY_REQUEST_NOT_SUPPORTED}, - {NULL, 0, 0} + { STRLEN("success"), SSH2_PUBLICKEY_SUCCESS }, + { STRLEN("access denied"), SSH2_PUBLICKEY_ACCESS_DENIED }, + { STRLEN("storage exceeded"), SSH2_PUBLICKEY_STORAGE_EXCEEDED }, + { STRLEN("version not supported"), SSH2_PUBLICKEY_VERSION_NOT_SUPPORTED }, + { STRLEN("key not found"), SSH2_PUBLICKEY_KEY_NOT_FOUND }, + { STRLEN("key not supported"), SSH2_PUBLICKEY_KEY_NOT_SUPPORTED }, + { STRLEN("key already present"), SSH2_PUBLICKEY_KEY_ALREADY_PRESENT }, + { STRLEN("general failure"), SSH2_PUBLICKEY_GENERAL_FAILURE }, + { STRLEN("request not supported"), SSH2_PUBLICKEY_REQUEST_NOT_SUPPORTED }, + { NULL, 0, 0 } }; #undef STRLEN @@ -107,14 +107,14 @@ static void publickey_status_error(const LIBSSH2_PUBLICKEY *pkey, status = 7; } - if(status > LIBSSH2_PUBLICKEY_STATUS_CODE_MAX) { + if(status > SSH2_PUBLICKEY_STATUS_CODE_MAX) { msg = "unknown"; } else { msg = publickey_status_codes[status].name; } - _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, msg); + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, msg); } /* @@ -130,41 +130,39 @@ static int publickey_packet_receive(LIBSSH2_PUBLICKEY *pkey, *data = NULL; /* default to nothing returned */ *data_len = 0; - if(pkey->receive_state == libssh2_NB_state_idle) { - rc = _libssh2_channel_read(channel, 0, (char *)buffer, 4); + if(pkey->receive_state == ssh2_NB_state_idle) { + rc = ssh2_channel_read(channel, 0, (char *)buffer, 4); if(rc == LIBSSH2_ERROR_EAGAIN) { return (int)rc; } else if(rc != 4) { - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Invalid response from publickey subsystem"); + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Invalid response from publickey subsystem"); } - pkey->receive_packet_len = _libssh2_ntohu32(buffer); - pkey->receive_packet = - LIBSSH2_ALLOC(session, pkey->receive_packet_len); + pkey->receive_packet_len = ssh2_ntohu32(buffer); + pkey->receive_packet = SSH2_ALLOC(session, pkey->receive_packet_len); if(!pkey->receive_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate publickey response " - "buffer"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate publickey response buffer"); } - pkey->receive_state = libssh2_NB_state_sent; + pkey->receive_state = ssh2_NB_state_sent; } - if(pkey->receive_state == libssh2_NB_state_sent) { - rc = _libssh2_channel_read(channel, 0, (char *)pkey->receive_packet, - pkey->receive_packet_len); + if(pkey->receive_state == ssh2_NB_state_sent) { + rc = ssh2_channel_read(channel, 0, (char *)pkey->receive_packet, + pkey->receive_packet_len); if(rc == LIBSSH2_ERROR_EAGAIN) { return (int)rc; } else if(rc != (ssize_t)pkey->receive_packet_len) { - LIBSSH2_FREE(session, pkey->receive_packet); + SSH2_FREE(session, pkey->receive_packet); pkey->receive_packet = NULL; - pkey->receive_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, - "Timeout waiting for publickey subsystem " - "response packet"); + pkey->receive_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, + "Timeout waiting for publickey subsystem " + "response packet"); } *data = pkey->receive_packet; @@ -173,7 +171,7 @@ static int publickey_packet_receive(LIBSSH2_PUBLICKEY *pkey, pkey->receive_packet_len = 0; } - pkey->receive_state = libssh2_NB_state_idle; + pkey->receive_state = ssh2_NB_state_idle; return 0; } @@ -192,7 +190,7 @@ static int publickey_response_id(unsigned char **pdata, size_t data_len) /* Malformed response */ return -1; } - response_len = _libssh2_ntohu32(data); + response_len = ssh2_ntohu32(data); data += 4; data_len -= 4; if(data_len < response_len) { @@ -228,49 +226,48 @@ static int publickey_response_success(LIBSSH2_PUBLICKEY *pkey) return rc; } else if(rc) { - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, - "Timeout waiting for response from " - "publickey subsystem"); + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, + "Timeout waiting for response from " + "publickey subsystem"); } if(data_len < 4) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Publickey response too small"); + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Publickey response too small"); } s = data; response = publickey_response_id(&s, data_len); switch(response) { - case LIBSSH2_PUBLICKEY_RESPONSE_STATUS: { + case SSH2_PUBLICKEY_RESPONSE_STATUS: { /* Error, or processing complete */ unsigned long status = 0; if(data_len < 8) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Publickey response too small"); + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Publickey response too small"); } - status = _libssh2_ntohu32(s); + status = ssh2_ntohu32(s); - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); - if(status == LIBSSH2_PUBLICKEY_SUCCESS) + if(status == SSH2_PUBLICKEY_SUCCESS) return 0; publickey_status_error(pkey, session, status); goto err_exit; } default: - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); if(response < 0) { - return _libssh2_error(session, - LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Invalid publickey subsystem response"); + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Invalid publickey subsystem response"); } /* Unknown/Unexpected */ - _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Unexpected publickey subsystem response"); + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Unexpected publickey subsystem response"); data = NULL; } } @@ -290,167 +287,165 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session) int response; int rc; - if(session->pkeyInit_state == libssh2_NB_state_idle) { + if(session->pkeyInit_state == ssh2_NB_state_idle) { session->pkeyInit_data = NULL; session->pkeyInit_pkey = NULL; session->pkeyInit_channel = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Initializing publickey subsystem")); + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, + "Initializing publickey subsystem")); - session->pkeyInit_state = libssh2_NB_state_allocated; + session->pkeyInit_state = ssh2_NB_state_allocated; } - if(session->pkeyInit_state == libssh2_NB_state_allocated) { + if(session->pkeyInit_state == ssh2_NB_state_allocated) { session->pkeyInit_channel = - _libssh2_channel_open(session, "session", - sizeof("session") - 1, - LIBSSH2_CHANNEL_WINDOW_DEFAULT, - LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, - 0); + ssh2_channel_open(session, "session", sizeof("session") - 1, + LIBSSH2_CHANNEL_WINDOW_DEFAULT, + LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0); if(!session->pkeyInit_channel) { if(libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) /* The error state is already set, so leave it */ return NULL; - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Unable to startup channel"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Unable to startup channel"); goto err_exit; } - session->pkeyInit_state = libssh2_NB_state_sent; + session->pkeyInit_state = ssh2_NB_state_sent; } - if(session->pkeyInit_state == libssh2_NB_state_sent) { - rc = _libssh2_channel_process_startup(session->pkeyInit_channel, + if(session->pkeyInit_state == ssh2_NB_state_sent) { + rc = ssh2_channel_process_startup(session->pkeyInit_channel, "subsystem", sizeof("subsystem") - 1, "publickey", sizeof("publickey") - 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block starting publickey subsystem"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block starting publickey subsystem"); return NULL; } else if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Unable to request publickey subsystem"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Unable to request publickey subsystem"); goto err_exit; } - session->pkeyInit_state = libssh2_NB_state_sent1; + session->pkeyInit_state = ssh2_NB_state_sent1; } - if(session->pkeyInit_state == libssh2_NB_state_sent1) { + if(session->pkeyInit_state == ssh2_NB_state_sent1) { unsigned char *s; - rc = _libssh2_channel_extended_data(session->pkeyInit_channel, - LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); + rc = ssh2_channel_extended_data(session->pkeyInit_channel, + LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block starting publickey subsystem"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block starting publickey subsystem"); return NULL; } session->pkeyInit_pkey = - LIBSSH2_CALLOC(session, sizeof(LIBSSH2_PUBLICKEY)); + SSH2_CALLOC(session, sizeof(LIBSSH2_PUBLICKEY)); if(!session->pkeyInit_pkey) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate a new publickey structure"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate a new publickey structure"); goto err_exit; } session->pkeyInit_pkey->channel = session->pkeyInit_channel; session->pkeyInit_pkey->version = 0; s = session->pkeyInit_buffer; - _libssh2_htonu32(s, 4 + (sizeof("version") - 1) + 4); + ssh2_htonu32(s, 4 + (sizeof("version") - 1) + 4); s += 4; - _libssh2_htonu32(s, sizeof("version") - 1); + ssh2_htonu32(s, sizeof("version") - 1); s += 4; memcpy(s, "version", sizeof("version") - 1); s += sizeof("version") - 1; - _libssh2_htonu32(s, LIBSSH2_PUBLICKEY_VERSION); + ssh2_htonu32(s, SSH2_PUBLICKEY_VERSION); session->pkeyInit_buffer_sent = 0; - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Sending publickey advertising version %d support", - (int)LIBSSH2_PUBLICKEY_VERSION)); + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, + "Sending publickey advertising version %d support", + (int)SSH2_PUBLICKEY_VERSION)); - session->pkeyInit_state = libssh2_NB_state_sent2; + session->pkeyInit_state = ssh2_NB_state_sent2; } - if(session->pkeyInit_state == libssh2_NB_state_sent2) { + if(session->pkeyInit_state == ssh2_NB_state_sent2) { ssize_t nwritten; - nwritten = _libssh2_channel_write(session->pkeyInit_channel, 0, - session->pkeyInit_buffer, - 19 - session->pkeyInit_buffer_sent); + nwritten = ssh2_channel_write(session->pkeyInit_channel, 0, + session->pkeyInit_buffer, + 19 - session->pkeyInit_buffer_sent); if(nwritten == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block sending publickey version packet"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block sending publickey version packet"); return NULL; } else if(nwritten < 0) { - _libssh2_error(session, (int)nwritten, - "Unable to send publickey version packet"); + ssh2_err(session, (int)nwritten, + "Unable to send publickey version packet"); goto err_exit; } session->pkeyInit_buffer_sent += nwritten; if(session->pkeyInit_buffer_sent < 19) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Need to be called again to complete this"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Need to be called again to complete this"); return NULL; } - session->pkeyInit_state = libssh2_NB_state_sent3; + session->pkeyInit_state = ssh2_NB_state_sent3; } - if(session->pkeyInit_state == libssh2_NB_state_sent3) { + if(session->pkeyInit_state == ssh2_NB_state_sent3) { for(;;) { unsigned char *s; rc = publickey_packet_receive(session->pkeyInit_pkey, &session->pkeyInit_data, &session->pkeyInit_data_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for response from " - "publickey subsystem"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for response from " + "publickey subsystem"); return NULL; } else if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, - "Timeout waiting for response from " - "publickey subsystem"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, + "Timeout waiting for response from " + "publickey subsystem"); goto err_exit; } s = session->pkeyInit_data; response = publickey_response_id(&s, session->pkeyInit_data_len); if(response < 0) { - _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Invalid publickey subsystem response code"); + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Invalid publickey subsystem response code"); goto err_exit; } if(session->pkeyInit_data_len < 4) { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Public key init data too small"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Public key init data too small"); goto err_exit; } switch(response) { - case LIBSSH2_PUBLICKEY_RESPONSE_STATUS: { + case SSH2_PUBLICKEY_RESPONSE_STATUS: { /* Error */ unsigned long status, descr_len, lang_len; if(session->pkeyInit_data_len >= 8) { - status = _libssh2_ntohu32(s); + status = ssh2_ntohu32(s); s += 4; - descr_len = _libssh2_ntohu32(s); + descr_len = ssh2_ntohu32(s); s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Public key init data too small"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Public key init data too small"); goto err_exit; } @@ -458,12 +453,12 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session) session->pkeyInit_data + session->pkeyInit_data_len) { /* description starts here */ s += descr_len; - lang_len = _libssh2_ntohu32(s); + lang_len = ssh2_ntohu32(s); s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Public key init data too small"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Public key init data too small"); goto err_exit; } @@ -473,16 +468,14 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session) s += lang_len; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Public key init data too small"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Public key init data too small"); goto err_exit; } - if(s > - session->pkeyInit_data + session->pkeyInit_data_len) { - _libssh2_error(session, - LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Malformed publickey subsystem packet"); + if(s > session->pkeyInit_data + session->pkeyInit_data_len) { + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Malformed publickey subsystem packet"); goto err_exit; } @@ -491,32 +484,28 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session) goto err_exit; } - case LIBSSH2_PUBLICKEY_RESPONSE_VERSION: + case SSH2_PUBLICKEY_RESPONSE_VERSION: /* What we want */ - session->pkeyInit_pkey->version = _libssh2_ntohu32(s); - if(session->pkeyInit_pkey->version > - LIBSSH2_PUBLICKEY_VERSION) { - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Truncate remote publickey version " - "from %u", - session->pkeyInit_pkey->version)); - session->pkeyInit_pkey->version = - LIBSSH2_PUBLICKEY_VERSION; + session->pkeyInit_pkey->version = ssh2_ntohu32(s); + if(session->pkeyInit_pkey->version > SSH2_PUBLICKEY_VERSION) { + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, + "Truncate remote publickey version from %u", + session->pkeyInit_pkey->version)); + session->pkeyInit_pkey->version = SSH2_PUBLICKEY_VERSION; } - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Enabling publickey subsystem version %u", - session->pkeyInit_pkey->version)); - LIBSSH2_FREE(session, session->pkeyInit_data); + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, + "Enabling publickey subsystem version %u", + session->pkeyInit_pkey->version)); + SSH2_FREE(session, session->pkeyInit_data); session->pkeyInit_data = NULL; - session->pkeyInit_state = libssh2_NB_state_idle; + session->pkeyInit_state = ssh2_NB_state_idle; return session->pkeyInit_pkey; default: /* Unknown/Unexpected */ - _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Unexpected publickey subsystem response, " - "ignoring"); - LIBSSH2_FREE(session, session->pkeyInit_data); + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Unexpected publickey subsystem response, ignoring"); + SSH2_FREE(session, session->pkeyInit_data); session->pkeyInit_data = NULL; } } @@ -524,24 +513,24 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session) /* Never reached except by direct goto */ err_exit: - session->pkeyInit_state = libssh2_NB_state_sent4; + session->pkeyInit_state = ssh2_NB_state_sent4; if(session->pkeyInit_channel) { - rc = _libssh2_channel_close(session->pkeyInit_channel); + rc = ssh2_channel_close(session->pkeyInit_channel); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block closing channel"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block closing channel"); return NULL; } } if(session->pkeyInit_pkey) { - LIBSSH2_FREE(session, session->pkeyInit_pkey); + SSH2_FREE(session, session->pkeyInit_pkey); session->pkeyInit_pkey = NULL; } if(session->pkeyInit_data) { - LIBSSH2_FREE(session, session->pkeyInit_data); + SSH2_FREE(session, session->pkeyInit_data); session->pkeyInit_data = NULL; } - session->pkeyInit_state = libssh2_NB_state_idle; + session->pkeyInit_state = ssh2_NB_state_idle; return NULL; } @@ -583,11 +572,11 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, channel = pkey->channel; session = channel->session; - if(pkey->add_state == libssh2_NB_state_idle) { + if(pkey->add_state == ssh2_NB_state_idle) { pkey->add_packet = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Adding %s publickey", name)); + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, "Adding %s publickey", + name)); if(pkey->version == 1) { for(i = 0; i < num_attrs; i++) { @@ -610,33 +599,33 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, } } - pkey->add_packet = LIBSSH2_ALLOC(session, packet_len); + pkey->add_packet = SSH2_ALLOC(session, packet_len); if(!pkey->add_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "publickey \"add\" packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "publickey \"add\" packet"); } pkey->add_s = pkey->add_packet; - _libssh2_htonu32(pkey->add_s, (uint32_t)(packet_len - 4)); + ssh2_htonu32(pkey->add_s, (uint32_t)(packet_len - 4)); pkey->add_s += 4; - _libssh2_htonu32(pkey->add_s, sizeof("add") - 1); + ssh2_htonu32(pkey->add_s, sizeof("add") - 1); pkey->add_s += 4; memcpy(pkey->add_s, "add", sizeof("add") - 1); pkey->add_s += sizeof("add") - 1; if(pkey->version == 1) { - _libssh2_htonu32(pkey->add_s, (uint32_t)comment_len); + ssh2_htonu32(pkey->add_s, (uint32_t)comment_len); pkey->add_s += 4; if(comment) { memcpy(pkey->add_s, comment, comment_len); pkey->add_s += comment_len; } - _libssh2_htonu32(pkey->add_s, (uint32_t)name_len); + ssh2_htonu32(pkey->add_s, (uint32_t)name_len); pkey->add_s += 4; memcpy(pkey->add_s, name, name_len); pkey->add_s += name_len; - _libssh2_htonu32(pkey->add_s, (uint32_t)blob_len); + ssh2_htonu32(pkey->add_s, (uint32_t)blob_len); pkey->add_s += 4; memcpy(pkey->add_s, blob, blob_len); pkey->add_s += blob_len; @@ -644,23 +633,23 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, else { /* Version == 2 */ - _libssh2_htonu32(pkey->add_s, (uint32_t)name_len); + ssh2_htonu32(pkey->add_s, (uint32_t)name_len); pkey->add_s += 4; memcpy(pkey->add_s, name, name_len); pkey->add_s += name_len; - _libssh2_htonu32(pkey->add_s, (uint32_t)blob_len); + ssh2_htonu32(pkey->add_s, (uint32_t)blob_len); pkey->add_s += 4; memcpy(pkey->add_s, blob, blob_len); pkey->add_s += blob_len; *(pkey->add_s++) = overwrite ? 0x01 : 0; - _libssh2_htonu32(pkey->add_s, (uint32_t)num_attrs); + ssh2_htonu32(pkey->add_s, (uint32_t)num_attrs); pkey->add_s += 4; for(i = 0; i < num_attrs; i++) { - _libssh2_htonu32(pkey->add_s, (uint32_t)attrs[i].name_len); + ssh2_htonu32(pkey->add_s, (uint32_t)attrs[i].name_len); pkey->add_s += 4; memcpy(pkey->add_s, attrs[i].name, attrs[i].name_len); pkey->add_s += attrs[i].name_len; - _libssh2_htonu32(pkey->add_s, (uint32_t)attrs[i].value_len); + ssh2_htonu32(pkey->add_s, (uint32_t)attrs[i].value_len); pkey->add_s += 4; memcpy(pkey->add_s, attrs[i].value, attrs[i].value_len); pkey->add_s += attrs[i].value_len; @@ -668,31 +657,31 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, } } - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Sending publickey \"add\" packet: " - "type=%s blob_len=%ld num_attrs=%ld", - name, blob_len, num_attrs)); + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, + "Sending publickey \"add\" packet: " + "type=%s blob_len=%ld num_attrs=%ld", + name, blob_len, num_attrs)); - pkey->add_state = libssh2_NB_state_created; + pkey->add_state = ssh2_NB_state_created; } - if(pkey->add_state == libssh2_NB_state_created) { + if(pkey->add_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, pkey->add_packet, - (pkey->add_s - pkey->add_packet)); + nwritten = ssh2_channel_write(channel, 0, pkey->add_packet, + (pkey->add_s - pkey->add_packet)); if(nwritten == LIBSSH2_ERROR_EAGAIN) { return (int)nwritten; } else if((pkey->add_s - pkey->add_packet) != nwritten) { - LIBSSH2_FREE(session, pkey->add_packet); + SSH2_FREE(session, pkey->add_packet); pkey->add_packet = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send publickey add packet"); + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send publickey add packet"); } - LIBSSH2_FREE(session, pkey->add_packet); + SSH2_FREE(session, pkey->add_packet); pkey->add_packet = NULL; - pkey->add_state = libssh2_NB_state_sent; + pkey->add_state = ssh2_NB_state_sent; } rc = publickey_response_success(pkey); @@ -700,7 +689,7 @@ int libssh2_publickey_add_ex(LIBSSH2_PUBLICKEY *pkey, return rc; } - pkey->add_state = libssh2_NB_state_idle; + pkey->add_state = ssh2_NB_state_idle; return rc; } @@ -729,58 +718,57 @@ int libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY *pkey, channel = pkey->channel; session = channel->session; - if(pkey->remove_state == libssh2_NB_state_idle) { + if(pkey->remove_state == ssh2_NB_state_idle) { pkey->remove_packet = NULL; - pkey->remove_packet = LIBSSH2_ALLOC(session, packet_len); + pkey->remove_packet = SSH2_ALLOC(session, packet_len); if(!pkey->remove_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "publickey \"remove\" packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "publickey \"remove\" packet"); } pkey->remove_s = pkey->remove_packet; - _libssh2_htonu32(pkey->remove_s, (uint32_t)(packet_len - 4)); + ssh2_htonu32(pkey->remove_s, (uint32_t)(packet_len - 4)); pkey->remove_s += 4; - _libssh2_htonu32(pkey->remove_s, sizeof("remove") - 1); + ssh2_htonu32(pkey->remove_s, sizeof("remove") - 1); pkey->remove_s += 4; memcpy(pkey->remove_s, "remove", sizeof("remove") - 1); pkey->remove_s += sizeof("remove") - 1; - _libssh2_htonu32(pkey->remove_s, (uint32_t)name_len); + ssh2_htonu32(pkey->remove_s, (uint32_t)name_len); pkey->remove_s += 4; memcpy(pkey->remove_s, name, name_len); pkey->remove_s += name_len; - _libssh2_htonu32(pkey->remove_s, (uint32_t)blob_len); + ssh2_htonu32(pkey->remove_s, (uint32_t)blob_len); pkey->remove_s += 4; memcpy(pkey->remove_s, blob, blob_len); pkey->remove_s += blob_len; - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Sending publickey \"remove\" packet: " - "type=%s blob_len=%ld", - name, blob_len)); + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, + "Sending publickey \"remove\" packet: " + "type=%s blob_len=%ld", name, blob_len)); - pkey->remove_state = libssh2_NB_state_created; + pkey->remove_state = ssh2_NB_state_created; } - if(pkey->remove_state == libssh2_NB_state_created) { + if(pkey->remove_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, pkey->remove_packet, - (pkey->remove_s - pkey->remove_packet)); + nwritten = ssh2_channel_write(channel, 0, pkey->remove_packet, + (pkey->remove_s - pkey->remove_packet)); if(nwritten == LIBSSH2_ERROR_EAGAIN) { return (int)nwritten; } else if((pkey->remove_s - pkey->remove_packet) != nwritten) { - LIBSSH2_FREE(session, pkey->remove_packet); + SSH2_FREE(session, pkey->remove_packet); pkey->remove_packet = NULL; - pkey->remove_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send publickey remove packet"); + pkey->remove_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send publickey remove packet"); } - LIBSSH2_FREE(session, pkey->remove_packet); + SSH2_FREE(session, pkey->remove_packet); pkey->remove_packet = NULL; - pkey->remove_state = libssh2_NB_state_sent; + pkey->remove_state = ssh2_NB_state_sent; } rc = publickey_response_success(pkey); @@ -788,7 +776,7 @@ int libssh2_publickey_remove_ex(LIBSSH2_PUBLICKEY *pkey, return rc; } - pkey->remove_state = libssh2_NB_state_idle; + pkey->remove_state = ssh2_NB_state_idle; return rc; } @@ -815,39 +803,39 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, channel = pkey->channel; session = channel->session; - if(pkey->listFetch_state == libssh2_NB_state_idle) { + if(pkey->listFetch_state == ssh2_NB_state_idle) { pkey->listFetch_data = NULL; pkey->listFetch_s = pkey->listFetch_buffer; - _libssh2_htonu32(pkey->listFetch_s, (uint32_t)(buffer_len - 4)); + ssh2_htonu32(pkey->listFetch_s, (uint32_t)(buffer_len - 4)); pkey->listFetch_s += 4; - _libssh2_htonu32(pkey->listFetch_s, sizeof("list") - 1); + ssh2_htonu32(pkey->listFetch_s, sizeof("list") - 1); pkey->listFetch_s += 4; memcpy(pkey->listFetch_s, "list", sizeof("list") - 1); pkey->listFetch_s += sizeof("list") - 1; - _libssh2_debug((session, LIBSSH2_TRACE_PUBLICKEY, - "Sending publickey \"list\" packet")); + ssh2_deb((session, LIBSSH2_TRACE_PUBLICKEY, + "Sending publickey \"list\" packet")); - pkey->listFetch_state = libssh2_NB_state_created; + pkey->listFetch_state = ssh2_NB_state_created; } - if(pkey->listFetch_state == libssh2_NB_state_created) { + if(pkey->listFetch_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, - pkey->listFetch_buffer, - (pkey->listFetch_s - - pkey->listFetch_buffer)); + nwritten = ssh2_channel_write(channel, 0, + pkey->listFetch_buffer, + (pkey->listFetch_s - + pkey->listFetch_buffer)); if(nwritten == LIBSSH2_ERROR_EAGAIN) { return (int)nwritten; } else if((pkey->listFetch_s - pkey->listFetch_buffer) != nwritten) { - pkey->listFetch_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send publickey list packet"); + pkey->listFetch_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send publickey list packet"); } - pkey->listFetch_state = libssh2_NB_state_sent; + pkey->listFetch_state = ssh2_NB_state_sent; } for(;;) { @@ -857,9 +845,8 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, return rc; } else if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, - "Timeout waiting for response from " - "publickey subsystem"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_TIMEOUT, + "Timeout waiting for response from publickey subsystem"); goto err_exit; } @@ -867,26 +854,26 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, response = publickey_response_id(&pkey->listFetch_s, pkey->listFetch_data_len); if(response < 0) { - _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Invalid publickey subsystem response code"); + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Invalid publickey subsystem response code"); goto err_exit; } switch(response) { - case LIBSSH2_PUBLICKEY_RESPONSE_STATUS: { + case SSH2_PUBLICKEY_RESPONSE_STATUS: { /* Error, or processing complete */ unsigned long status, descr_len, lang_len; if(pkey->listFetch_s + 8 <= pkey->listFetch_data + pkey->listFetch_data_len) { - status = _libssh2_ntohu32(pkey->listFetch_s); + status = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; - descr_len = _libssh2_ntohu32(pkey->listFetch_s); + descr_len = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -894,12 +881,12 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_data + pkey->listFetch_data_len) { /* description starts at pkey->listFetch_s */ pkey->listFetch_s += descr_len; - lang_len = _libssh2_ntohu32(pkey->listFetch_s); + lang_len = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -909,44 +896,42 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_s += lang_len; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } if(pkey->listFetch_s > pkey->listFetch_data + pkey->listFetch_data_len) { - _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Malformed publickey subsystem packet"); + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Malformed publickey subsystem packet"); goto err_exit; } - if(status == LIBSSH2_PUBLICKEY_SUCCESS) { - LIBSSH2_FREE(session, pkey->listFetch_data); + if(status == SSH2_PUBLICKEY_SUCCESS) { + SSH2_FREE(session, pkey->listFetch_data); pkey->listFetch_data = NULL; *pkey_list = list; *num_keys = keys; - pkey->listFetch_state = libssh2_NB_state_idle; + pkey->listFetch_state = ssh2_NB_state_idle; return 0; } publickey_status_error(pkey, session, status); goto err_exit; } - case LIBSSH2_PUBLICKEY_RESPONSE_PUBLICKEY: + case SSH2_PUBLICKEY_RESPONSE_PUBLICKEY: /* What we want */ if(keys >= max_keys) { libssh2_publickey_list *newlist; /* Grow the key list if necessary */ max_keys += 8; - newlist = - LIBSSH2_REALLOC(session, list, - (max_keys + - 1) * sizeof(libssh2_publickey_list)); + newlist = SSH2_REALLOC(session, list, + (max_keys + 1) * + sizeof(libssh2_publickey_list)); if(!newlist) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "publickey list"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for publickey list"); goto err_exit; } list = newlist; @@ -956,24 +941,24 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { - comment_len = _libssh2_ntohu32(pkey->listFetch_s); + comment_len = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } if(comment_len) { list[keys].num_attrs = 1; list[keys].attrs = - LIBSSH2_ALLOC(session, - sizeof(libssh2_publickey_attribute)); + SSH2_ALLOC(session, + sizeof(libssh2_publickey_attribute)); if(!list[keys].attrs) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "publickey attributes"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "publickey attributes"); goto err_exit; } list[keys].attrs[0].name = "comment"; @@ -991,12 +976,12 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { - list[keys].name_len = _libssh2_ntohu32(pkey->listFetch_s); + list[keys].name_len = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -1006,19 +991,19 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_s += list[keys].name_len; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { - list[keys].blob_len = _libssh2_ntohu32(pkey->listFetch_s); + list[keys].blob_len = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -1028,8 +1013,8 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_s += list[keys].blob_len; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } } @@ -1038,12 +1023,12 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { - list[keys].name_len = _libssh2_ntohu32(pkey->listFetch_s); + list[keys].name_len = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -1053,19 +1038,19 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_s += list[keys].name_len; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { - list[keys].blob_len = _libssh2_ntohu32(pkey->listFetch_s); + list[keys].blob_len = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -1075,44 +1060,43 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_s += list[keys].blob_len; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { - list[keys].num_attrs = _libssh2_ntohu32(pkey->listFetch_s); + list[keys].num_attrs = ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } if(list[keys].num_attrs) { list[keys].attrs = - LIBSSH2_ALLOC(session, - list[keys].num_attrs * - sizeof(libssh2_publickey_attribute)); + SSH2_ALLOC(session, + list[keys].num_attrs * + sizeof(libssh2_publickey_attribute)); if(!list[keys].attrs) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "publickey attributes"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "publickey attributes"); goto err_exit; } for(i = 0; i < list[keys].num_attrs; i++) { if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { list[keys].attrs[i].name_len = - _libssh2_ntohu32(pkey->listFetch_s); + ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, - LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -1123,22 +1107,20 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_s += list[keys].attrs[i].name_len; } else { - _libssh2_error(session, - LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } if(pkey->listFetch_s + 4 <= pkey->listFetch_data + pkey->listFetch_data_len) { list[keys].attrs[i].value_len = - _libssh2_ntohu32(pkey->listFetch_s); + ssh2_ntohu32(pkey->listFetch_s); pkey->listFetch_s += 4; } else { - _libssh2_error(session, - LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -1149,9 +1131,8 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, pkey->listFetch_s += list[keys].attrs[i].value_len; } else { - _libssh2_error(session, - LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "ListFetch data too short"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "ListFetch data too short"); goto err_exit; } @@ -1172,9 +1153,9 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, break; default: /* Unknown/Unexpected */ - _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Unexpected publickey subsystem response"); - LIBSSH2_FREE(session, pkey->listFetch_data); + ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Unexpected publickey subsystem response"); + SSH2_FREE(session, pkey->listFetch_data); pkey->listFetch_data = NULL; } } @@ -1182,13 +1163,13 @@ int libssh2_publickey_list_fetch(LIBSSH2_PUBLICKEY *pkey, /* Only reached via explicit goto */ err_exit: if(pkey->listFetch_data) { - LIBSSH2_FREE(session, pkey->listFetch_data); + SSH2_FREE(session, pkey->listFetch_data); pkey->listFetch_data = NULL; } if(list) { libssh2_publickey_list_free(pkey, list); } - pkey->listFetch_state = libssh2_NB_state_idle; + pkey->listFetch_state = ssh2_NB_state_idle; return -1; } @@ -1209,13 +1190,13 @@ void libssh2_publickey_list_free(LIBSSH2_PUBLICKEY *pkey, while(p->packet) { if(p->attrs) { - LIBSSH2_FREE(session, p->attrs); + SSH2_FREE(session, p->attrs); } - LIBSSH2_FREE(session, p->packet); + SSH2_FREE(session, p->packet); p++; } - LIBSSH2_FREE(session, pkey_list); + SSH2_FREE(session, pkey_list); } /* @@ -1236,26 +1217,26 @@ int libssh2_publickey_shutdown(LIBSSH2_PUBLICKEY *pkey) * Make sure all memory used in the state variables are free */ if(pkey->receive_packet) { - LIBSSH2_FREE(session, pkey->receive_packet); + SSH2_FREE(session, pkey->receive_packet); pkey->receive_packet = NULL; } if(pkey->add_packet) { - LIBSSH2_FREE(session, pkey->add_packet); + SSH2_FREE(session, pkey->add_packet); pkey->add_packet = NULL; } if(pkey->remove_packet) { - LIBSSH2_FREE(session, pkey->remove_packet); + SSH2_FREE(session, pkey->remove_packet); pkey->remove_packet = NULL; } if(pkey->listFetch_data) { - LIBSSH2_FREE(session, pkey->listFetch_data); + SSH2_FREE(session, pkey->listFetch_data); pkey->listFetch_data = NULL; } - rc = _libssh2_channel_free(pkey->channel); + rc = ssh2_channel_free(pkey->channel); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; - LIBSSH2_FREE(session, pkey); + SSH2_FREE(session, pkey); return 0; } diff --git a/src/scp.c b/src/scp.c index a818b1be..b045740b 100644 --- a/src/scp.c +++ b/src/scp.c @@ -53,8 +53,8 @@ #define scpsize_strtol strtol #endif -/* Max. length of a quoted string after libssh2_shell_quotearg() processing */ -#define _libssh2_shell_quotedsize(s) (3 * strlen(s) + 2) +/* Max. length of a quoted string after shell_quotearg() processing */ +#define shell_quotedsize(s) (3 * strlen(s) + 2) /* This function quotes a string in a way suitable to be used with a @@ -284,27 +284,26 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, const char *tmp_err_msg; if(!path) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Path argument can not be null"); + ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Path argument can not be null"); return NULL; } - if(session->scpRecv_state == libssh2_NB_state_idle) { + if(session->scpRecv_state == ssh2_NB_state_idle) { session->scpRecv_mode = 0; session->scpRecv_size = 0; session->scpRecv_mtime = 0; session->scpRecv_atime = 0; session->scpRecv_command_len = - _libssh2_shell_quotedsize(path) + sizeof("scp -f ") + (sb ? 1 : 0); + shell_quotedsize(path) + sizeof("scp -f ") + (sb ? 1 : 0); session->scpRecv_command = - LIBSSH2_ALLOC(session, session->scpRecv_command_len); + SSH2_ALLOC(session, session->scpRecv_command_len); if(!session->scpRecv_command) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate a command buffer for " - "SCP session"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate a command buffer for SCP session"); return NULL; } @@ -331,69 +330,67 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, /* the command to exec should _not_ be null-terminated */ session->scpRecv_command_len = cmd_len; - _libssh2_debug((session, LIBSSH2_TRACE_SCP, - "Opening channel for SCP receive")); + ssh2_deb((session, LIBSSH2_TRACE_SCP, + "Opening channel for SCP receive")); - session->scpRecv_state = libssh2_NB_state_created; + session->scpRecv_state = ssh2_NB_state_created; } - if(session->scpRecv_state == libssh2_NB_state_created) { + if(session->scpRecv_state == ssh2_NB_state_created) { /* Allocate a channel */ session->scpRecv_channel = - _libssh2_channel_open(session, "session", - sizeof("session") - 1, - LIBSSH2_CHANNEL_WINDOW_DEFAULT, - LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, - 0); + ssh2_channel_open(session, "session", + sizeof("session") - 1, + LIBSSH2_CHANNEL_WINDOW_DEFAULT, + LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0); if(!session->scpRecv_channel) { - if(libssh2_session_last_errno(session) != - LIBSSH2_ERROR_EAGAIN) { - LIBSSH2_FREE(session, session->scpRecv_command); + if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { + SSH2_FREE(session, session->scpRecv_command); session->scpRecv_command = NULL; - session->scpRecv_state = libssh2_NB_state_idle; + session->scpRecv_state = ssh2_NB_state_idle; } else { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block starting up channel"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block starting up channel"); } return NULL; } - session->scpRecv_state = libssh2_NB_state_sent; + session->scpRecv_state = ssh2_NB_state_sent; } - if(session->scpRecv_state == libssh2_NB_state_sent) { + if(session->scpRecv_state == ssh2_NB_state_sent) { /* Request SCP for the desired file */ - rc = _libssh2_channel_process_startup(session->scpRecv_channel, "exec", - sizeof("exec") - 1, - (char *)session->scpRecv_command, - session->scpRecv_command_len); + rc = ssh2_channel_process_startup(session->scpRecv_channel, "exec", + sizeof("exec") - 1, + (char *)session->scpRecv_command, + session->scpRecv_command_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block requesting SCP startup"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block requesting SCP startup"); return NULL; } else if(rc) { - LIBSSH2_FREE(session, session->scpRecv_command); + SSH2_FREE(session, session->scpRecv_command); session->scpRecv_command = NULL; goto scp_recv_error; } - LIBSSH2_FREE(session, session->scpRecv_command); + SSH2_FREE(session, session->scpRecv_command); session->scpRecv_command = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_SCP, "Sending initial wakeup")); + ssh2_deb((session, LIBSSH2_TRACE_SCP, "Sending initial wakeup")); /* SCP ACK */ session->scpRecv_response[0] = '\0'; - session->scpRecv_state = libssh2_NB_state_sent1; + session->scpRecv_state = ssh2_NB_state_sent1; } - if(session->scpRecv_state == libssh2_NB_state_sent1) { - rc = (int)_libssh2_channel_write(session->scpRecv_channel, 0, - session->scpRecv_response, 1); + if(session->scpRecv_state == ssh2_NB_state_sent1) { + rc = (int)ssh2_channel_write(session->scpRecv_channel, 0, + session->scpRecv_response, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block sending initial wakeup"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block sending initial wakeup"); return NULL; } else if(rc != 1) { @@ -403,29 +400,28 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, /* Parse SCP response */ session->scpRecv_response_len = 0; - session->scpRecv_state = libssh2_NB_state_sent2; + session->scpRecv_state = ssh2_NB_state_sent2; } - if((session->scpRecv_state == libssh2_NB_state_sent2) || - (session->scpRecv_state == libssh2_NB_state_sent3)) { + if((session->scpRecv_state == ssh2_NB_state_sent2) || + (session->scpRecv_state == ssh2_NB_state_sent3)) { while(sb && (session->scpRecv_response_len < - LIBSSH2_SCP_RESPONSE_BUFLEN)) { + SSH2_SCP_RESPONSE_BUFLEN)) { unsigned char *s, *p; - if(session->scpRecv_state == libssh2_NB_state_sent2) { - rc = (int)_libssh2_channel_read(session->scpRecv_channel, 0, - (char *)session-> - scpRecv_response + - session->scpRecv_response_len, - 1); + if(session->scpRecv_state == ssh2_NB_state_sent2) { + rc = (int)ssh2_channel_read(session->scpRecv_channel, 0, + (char *)session-> + scpRecv_response + + session->scpRecv_response_len, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for SCP response"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for SCP response"); return NULL; } else if(rc < 0) { /* error, give up */ - _libssh2_error(session, rc, "Failed reading SCP response"); + ssh2_err(session, rc, "Failed reading SCP response"); goto scp_recv_error; } else if(rc == 0) @@ -444,30 +440,30 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, The following string MUST be newline terminated */ err_len = - _libssh2_channel_packet_data_len(session-> - scpRecv_channel, 0); - err_msg = LIBSSH2_ALLOC(session, err_len + 1); + ssh2_channel_packet_data_len(session->scpRecv_channel, + 0); + err_msg = SSH2_ALLOC(session, err_len + 1); if(!err_msg) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed to get memory "); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed to get memory "); goto scp_recv_error; } /* Read the remote error message */ - (void)_libssh2_channel_read(session->scpRecv_channel, 0, - err_msg, err_len); + (void)ssh2_channel_read(session->scpRecv_channel, 0, + err_msg, err_len); /* If it failed for any reason, we ignore it anyway. */ /* null-terminate the error */ err_msg[err_len] = 0; - _libssh2_debug((session, LIBSSH2_TRACE_SCP, "got %02x %s", - session->scpRecv_response[0], err_msg)); + ssh2_deb((session, LIBSSH2_TRACE_SCP, "got %02x %s", + session->scpRecv_response[0], err_msg)); - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Failed to recv file"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Failed to recv file"); - LIBSSH2_FREE(session, err_msg); + SSH2_FREE(session, err_msg); goto scp_recv_error; } @@ -482,8 +478,8 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, 1] != '\r') && (session->scpRecv_response[session->scpRecv_response_len - 1] != '\n')) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid data in SCP response"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid data in SCP response"); goto scp_recv_error; } @@ -491,11 +487,10 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, (session->scpRecv_response[session->scpRecv_response_len - 1] != '\n')) { if(session->scpRecv_response_len == - LIBSSH2_SCP_RESPONSE_BUFLEN) { + SSH2_SCP_RESPONSE_BUFLEN) { /* You had your chance */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Unterminated response from " - "SCP server"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Unterminated response from SCP server"); goto scp_recv_error; } /* Way too short to be an SCP response, or not done yet, @@ -516,9 +511,8 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, if(session->scpRecv_response_len < 8) { /* EOL came too soon */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "too short"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, too short"); goto scp_recv_error; } @@ -527,9 +521,9 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, p = (unsigned char *)strchr((char *)s, ' '); if(!p || ((p - s) <= 0)) { /* No spaces or space in the wrong spot */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "malformed mtime"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, " + "malformed mtime"); goto scp_recv_error; } @@ -540,9 +534,9 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, s = (unsigned char *)strchr((char *)p, ' '); if(!s || ((s - p) <= 0)) { /* No spaces or space in the wrong spot */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "malformed mtime.usec"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, " + "malformed mtime.usec"); goto scp_recv_error; } @@ -551,9 +545,9 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, p = (unsigned char *)strchr((char *)s, ' '); if(!p || ((p - s) <= 0)) { /* No spaces or space in the wrong spot */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "too short or malformed"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, " + "too short or malformed"); goto scp_recv_error; } @@ -564,25 +558,25 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, /* SCP ACK */ session->scpRecv_response[0] = '\0'; - session->scpRecv_state = libssh2_NB_state_sent3; + session->scpRecv_state = ssh2_NB_state_sent3; } - if(session->scpRecv_state == libssh2_NB_state_sent3) { - rc = (int)_libssh2_channel_write(session->scpRecv_channel, 0, - session->scpRecv_response, 1); + if(session->scpRecv_state == ssh2_NB_state_sent3) { + rc = (int)ssh2_channel_write(session->scpRecv_channel, 0, + session->scpRecv_response, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting to send SCP ACK"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting to send SCP ACK"); return NULL; } else if(rc != 1) { goto scp_recv_error; } - _libssh2_debug((session, LIBSSH2_TRACE_SCP, - "mtime = %ld, atime = %ld", - session->scpRecv_mtime, - session->scpRecv_atime)); + ssh2_deb((session, LIBSSH2_TRACE_SCP, + "mtime = %ld, atime = %ld", + session->scpRecv_mtime, + session->scpRecv_atime)); /* We *should* check that atime.usec is valid, but why let that stop use? */ @@ -590,34 +584,33 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, } } - session->scpRecv_state = libssh2_NB_state_sent4; + session->scpRecv_state = ssh2_NB_state_sent4; } - if(session->scpRecv_state == libssh2_NB_state_sent4) { + if(session->scpRecv_state == ssh2_NB_state_sent4) { session->scpRecv_response_len = 0; - session->scpRecv_state = libssh2_NB_state_sent5; + session->scpRecv_state = ssh2_NB_state_sent5; } - if((session->scpRecv_state == libssh2_NB_state_sent5) || - (session->scpRecv_state == libssh2_NB_state_sent6)) { - while(session->scpRecv_response_len < LIBSSH2_SCP_RESPONSE_BUFLEN) { + if((session->scpRecv_state == ssh2_NB_state_sent5) || + (session->scpRecv_state == ssh2_NB_state_sent6)) { + while(session->scpRecv_response_len < SSH2_SCP_RESPONSE_BUFLEN) { char *s, *p, *e = NULL; - if(session->scpRecv_state == libssh2_NB_state_sent5) { - rc = (int)_libssh2_channel_read(session->scpRecv_channel, 0, - (char *)session-> - scpRecv_response + - session->scpRecv_response_len, - 1); + if(session->scpRecv_state == ssh2_NB_state_sent5) { + rc = (int)ssh2_channel_read(session->scpRecv_channel, 0, + (char *)session-> + scpRecv_response + + session->scpRecv_response_len, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for SCP response"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for SCP response"); return NULL; } else if(rc < 0) { /* error, bail out */ - _libssh2_error(session, rc, "Failed reading SCP response"); + ssh2_err(session, rc, "Failed reading SCP response"); goto scp_recv_error; } else if(rc == 0) @@ -626,8 +619,8 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, session->scpRecv_response_len++; if(session->scpRecv_response[0] != 'C') { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server"); goto scp_recv_error; } @@ -638,8 +631,8 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, 1] != '\n') && (session->scpRecv_response[session->scpRecv_response_len - 1] < 32)) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid data in SCP response"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid data in SCP response"); goto scp_recv_error; } @@ -647,11 +640,10 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, (session->scpRecv_response[session->scpRecv_response_len - 1] != '\n')) { if(session->scpRecv_response_len == - LIBSSH2_SCP_RESPONSE_BUFLEN) { + SSH2_SCP_RESPONSE_BUFLEN) { /* You had your chance */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Unterminated response " - "from SCP server"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Unterminated response from SCP server"); goto scp_recv_error; } /* Way too short to be an SCP response, or not done yet, @@ -673,9 +665,8 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, if(session->scpRecv_response_len < 6) { /* EOL came too soon */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "too short"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, too short"); goto scp_recv_error; } @@ -684,9 +675,9 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, p = strchr(s, ' '); if(!p || ((p - s) <= 0)) { /* No spaces or space in the wrong spot */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "malformed mode"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, " + "malformed mode"); goto scp_recv_error; } @@ -694,18 +685,17 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, /* Make sure we do not get fooled by leftover values */ session->scpRecv_mode = strtol(s, &e, 8); if(e && *e) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "invalid mode"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, invalid mode"); goto scp_recv_error; } s = strchr(p, ' '); if(!s || ((s - p) <= 0)) { /* No spaces or space in the wrong spot */ - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "too short or malformed"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, " + "too short or malformed"); goto scp_recv_error; } @@ -713,33 +703,31 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, /* Make sure we do not get fooled by leftover values */ session->scpRecv_size = scpsize_strtol(p, &e, 10); if(e && *e) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid response from SCP server, " - "invalid size"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid response from SCP server, invalid size"); goto scp_recv_error; } /* SCP ACK */ session->scpRecv_response[0] = '\0'; - session->scpRecv_state = libssh2_NB_state_sent6; + session->scpRecv_state = ssh2_NB_state_sent6; } - if(session->scpRecv_state == libssh2_NB_state_sent6) { - rc = (int)_libssh2_channel_write(session->scpRecv_channel, 0, - session->scpRecv_response, 1); + if(session->scpRecv_state == ssh2_NB_state_sent6) { + rc = (int)ssh2_channel_write(session->scpRecv_channel, 0, + session->scpRecv_response, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block sending SCP ACK"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block sending SCP ACK"); return NULL; } else if(rc != 1) { goto scp_recv_error; } - _libssh2_debug((session, LIBSSH2_TRACE_SCP, - "mode = 0%lo size = %ld", - session->scpRecv_mode, - (long)session->scpRecv_size)); + ssh2_deb((session, LIBSSH2_TRACE_SCP, "mode = 0%lo size = %ld", + session->scpRecv_mode, + (long)session->scpRecv_size)); /* We *should* check that basename is valid, but why let that stop us? */ @@ -747,7 +735,7 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, } } - session->scpRecv_state = libssh2_NB_state_sent7; + session->scpRecv_state = ssh2_NB_state_sent7; } if(sb) { @@ -759,15 +747,15 @@ static LIBSSH2_CHANNEL *scp_recv(LIBSSH2_SESSION *session, sb->st_mode = (unsigned short)session->scpRecv_mode; } - session->scpRecv_state = libssh2_NB_state_idle; + session->scpRecv_state = ssh2_NB_state_idle; return session->scpRecv_channel; scp_recv_empty_channel: /* the code only jumps here as a result of a zero read from channel_read() so we check EOF status to avoid getting stuck in a loop */ if(libssh2_channel_eof(session->scpRecv_channel)) - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Unexpected channel close"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Unexpected channel close"); else return session->scpRecv_channel; /* fall-through */ @@ -780,7 +768,7 @@ scp_recv_error: session->err_code = tmp_err_code; session->err_msg = tmp_err_msg; session->scpRecv_channel = NULL; - session->scpRecv_state = libssh2_NB_state_idle; + session->scpRecv_state = ssh2_NB_state_idle; return NULL; } @@ -848,23 +836,22 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session, const char *tmp_err_msg; if(!path) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Path argument can not be null"); + ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Path argument can not be null"); return NULL; } - if(session->scpSend_state == libssh2_NB_state_idle) { + if(session->scpSend_state == ssh2_NB_state_idle) { session->scpSend_command_len = - _libssh2_shell_quotedsize(path) + sizeof("scp -t ") + + shell_quotedsize(path) + sizeof("scp -t ") + ((mtime || atime) ? 1 : 0); session->scpSend_command = - LIBSSH2_ALLOC(session, session->scpSend_command_len); + SSH2_ALLOC(session, session->scpSend_command_len); if(!session->scpSend_command) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate a command buffer for " - "SCP session"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate a command buffer for SCP session"); return NULL; } @@ -891,149 +878,147 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session, /* the command to exec should _not_ be null-terminated */ session->scpSend_command_len = cmd_len; - _libssh2_debug((session, LIBSSH2_TRACE_SCP, - "Opening channel for SCP send")); + ssh2_deb((session, LIBSSH2_TRACE_SCP, "Opening channel for SCP send")); /* Allocate a channel */ - session->scpSend_state = libssh2_NB_state_created; + session->scpSend_state = ssh2_NB_state_created; } - if(session->scpSend_state == libssh2_NB_state_created) { + if(session->scpSend_state == ssh2_NB_state_created) { session->scpSend_channel = - _libssh2_channel_open(session, "session", sizeof("session") - 1, - LIBSSH2_CHANNEL_WINDOW_DEFAULT, - LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0); + ssh2_channel_open(session, "session", sizeof("session") - 1, + LIBSSH2_CHANNEL_WINDOW_DEFAULT, + LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0); if(!session->scpSend_channel) { if(libssh2_session_last_errno(session) != LIBSSH2_ERROR_EAGAIN) { /* previous call set libssh2_session_last_error(), pass it through */ - LIBSSH2_FREE(session, session->scpSend_command); + SSH2_FREE(session, session->scpSend_command); session->scpSend_command = NULL; - session->scpSend_state = libssh2_NB_state_idle; + session->scpSend_state = ssh2_NB_state_idle; } else { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block starting up channel"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block starting up channel"); } return NULL; } - session->scpSend_state = libssh2_NB_state_sent; + session->scpSend_state = ssh2_NB_state_sent; } - if(session->scpSend_state == libssh2_NB_state_sent) { + if(session->scpSend_state == ssh2_NB_state_sent) { /* Request SCP for the desired file */ - rc = _libssh2_channel_process_startup(session->scpSend_channel, "exec", - sizeof("exec") - 1, - (char *)session->scpSend_command, - session->scpSend_command_len); + rc = ssh2_channel_process_startup(session->scpSend_channel, "exec", + sizeof("exec") - 1, + (char *)session->scpSend_command, + session->scpSend_command_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block requesting SCP startup"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block requesting SCP startup"); return NULL; } else if(rc) { /* previous call set libssh2_session_last_error(), pass it through */ - LIBSSH2_FREE(session, session->scpSend_command); + SSH2_FREE(session, session->scpSend_command); session->scpSend_command = NULL; - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Unknown error while getting error string"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Unknown error while getting error string"); goto scp_send_error; } - LIBSSH2_FREE(session, session->scpSend_command); + SSH2_FREE(session, session->scpSend_command); session->scpSend_command = NULL; - session->scpSend_state = libssh2_NB_state_sent1; + session->scpSend_state = ssh2_NB_state_sent1; } - if(session->scpSend_state == libssh2_NB_state_sent1) { + if(session->scpSend_state == ssh2_NB_state_sent1) { /* Wait for ACK */ - rc = (int)_libssh2_channel_read(session->scpSend_channel, 0, - (char *)session->scpSend_response, 1); + rc = (int)ssh2_channel_read(session->scpSend_channel, 0, + (char *)session->scpSend_response, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for response from remote"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for response from remote"); return NULL; } else if(rc < 0) { - _libssh2_error(session, rc, "SCP failure"); + ssh2_err(session, rc, "SCP failure"); goto scp_send_error; } else if(!rc) /* remain in the same state */ goto scp_send_empty_channel; else if(session->scpSend_response[0]) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid ACK response from remote"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid ACK response from remote"); goto scp_send_error; } if(mtime || atime) { /* Send mtime and atime to be used for file */ session->scpSend_response_len = snprintf((char *)session->scpSend_response, - LIBSSH2_SCP_RESPONSE_BUFLEN, "T%ld 0 %ld 0\n", + SSH2_SCP_RESPONSE_BUFLEN, "T%ld 0 %ld 0\n", (long)mtime, (long)atime); - _libssh2_debug((session, LIBSSH2_TRACE_SCP, "Sent %s", - session->scpSend_response)); + ssh2_deb((session, LIBSSH2_TRACE_SCP, "Sent %s", + session->scpSend_response)); } - session->scpSend_state = libssh2_NB_state_sent2; + session->scpSend_state = ssh2_NB_state_sent2; } /* Send mtime and atime to be used for file */ if(mtime || atime) { - if(session->scpSend_state == libssh2_NB_state_sent2) { - rc = (int)_libssh2_channel_write(session->scpSend_channel, 0, - session->scpSend_response, - session->scpSend_response_len); + if(session->scpSend_state == ssh2_NB_state_sent2) { + rc = (int)ssh2_channel_write(session->scpSend_channel, 0, + session->scpSend_response, + session->scpSend_response_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block sending time data for SCP file"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block sending time data for SCP file"); return NULL; } else if(rc != (int)session->scpSend_response_len) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send time data for SCP file"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send time data for SCP file"); goto scp_send_error; } - session->scpSend_state = libssh2_NB_state_sent3; + session->scpSend_state = ssh2_NB_state_sent3; } - if(session->scpSend_state == libssh2_NB_state_sent3) { + if(session->scpSend_state == ssh2_NB_state_sent3) { /* Wait for ACK */ - rc = (int)_libssh2_channel_read(session->scpSend_channel, 0, - (char *)session->scpSend_response, - 1); + rc = (int)ssh2_channel_read(session->scpSend_channel, 0, + (char *)session->scpSend_response, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for response"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for response"); return NULL; } else if(rc < 0) { - _libssh2_error(session, rc, "SCP failure"); + ssh2_err(session, rc, "SCP failure"); goto scp_send_error; } else if(!rc) /* remain in the same state */ goto scp_send_empty_channel; else if(session->scpSend_response[0]) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid SCP ACK response"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid SCP ACK response"); goto scp_send_error; } - session->scpSend_state = libssh2_NB_state_sent4; + session->scpSend_state = ssh2_NB_state_sent4; } } else { - if(session->scpSend_state == libssh2_NB_state_sent2) { - session->scpSend_state = libssh2_NB_state_sent4; + if(session->scpSend_state == ssh2_NB_state_sent2) { + session->scpSend_state = ssh2_NB_state_sent4; } } - if(session->scpSend_state == libssh2_NB_state_sent4) { + if(session->scpSend_state == ssh2_NB_state_sent4) { /* Send mode, size, and basename */ const char *base = strrchr(path, '/'); if(base) @@ -1043,45 +1028,44 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session, session->scpSend_response_len = snprintf((char *)session->scpSend_response, - LIBSSH2_SCP_RESPONSE_BUFLEN, "C0%o %" - LIBSSH2_INT64_T_FORMAT " %s\n", mode, - size, base); - _libssh2_debug((session, LIBSSH2_TRACE_SCP, "Sent %s", - session->scpSend_response)); + SSH2_SCP_RESPONSE_BUFLEN, + "C0%o %" SSH2_INT64_T_FORMAT " %s\n", mode, size, base); + ssh2_deb((session, LIBSSH2_TRACE_SCP, "Sent %s", + session->scpSend_response)); - session->scpSend_state = libssh2_NB_state_sent5; + session->scpSend_state = ssh2_NB_state_sent5; } - if(session->scpSend_state == libssh2_NB_state_sent5) { - rc = (int)_libssh2_channel_write(session->scpSend_channel, 0, - session->scpSend_response, - session->scpSend_response_len); + if(session->scpSend_state == ssh2_NB_state_sent5) { + rc = (int)ssh2_channel_write(session->scpSend_channel, 0, + session->scpSend_response, + session->scpSend_response_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block send core file data for SCP file"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block send core file data for SCP file"); return NULL; } else if(rc != (int)session->scpSend_response_len) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send core file data for SCP file"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send core file data for SCP file"); goto scp_send_error; } - session->scpSend_state = libssh2_NB_state_sent6; + session->scpSend_state = ssh2_NB_state_sent6; } - if(session->scpSend_state == libssh2_NB_state_sent6) { + if(session->scpSend_state == ssh2_NB_state_sent6) { /* Wait for ACK */ - rc = (int)_libssh2_channel_read(session->scpSend_channel, 0, - (char *)session->scpSend_response, 1); + rc = (int)ssh2_channel_read(session->scpSend_channel, 0, + (char *)session->scpSend_response, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for response"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for response"); return NULL; } else if(rc < 0) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Invalid ACK response from remote"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Invalid ACK response from remote"); goto scp_send_error; } else if(rc == 0) @@ -1092,38 +1076,37 @@ static LIBSSH2_CHANNEL *scp_send(LIBSSH2_SESSION *session, char *err_msg; err_len = - _libssh2_channel_packet_data_len(session->scpSend_channel, 0); - err_msg = LIBSSH2_ALLOC(session, err_len + 1); + ssh2_channel_packet_data_len(session->scpSend_channel, 0); + err_msg = SSH2_ALLOC(session, err_len + 1); if(!err_msg) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "failed to get memory"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, "failed to get memory"); goto scp_send_error; } /* Read the remote error message */ - rc = (int)_libssh2_channel_read(session->scpSend_channel, 0, - err_msg, err_len); + rc = (int)ssh2_channel_read(session->scpSend_channel, 0, + err_msg, err_len); if(rc > 0) { err_msg[err_len] = 0; - _libssh2_debug((session, LIBSSH2_TRACE_SCP, "got %02x %s", - session->scpSend_response[0], err_msg)); + ssh2_deb((session, LIBSSH2_TRACE_SCP, "got %02x %s", + session->scpSend_response[0], err_msg)); } - LIBSSH2_FREE(session, err_msg); - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "failed to send file"); + SSH2_FREE(session, err_msg); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "failed to send file"); goto scp_send_error; } } - session->scpSend_state = libssh2_NB_state_idle; + session->scpSend_state = ssh2_NB_state_idle; return session->scpSend_channel; scp_send_empty_channel: /* the code only jumps here as a result of a zero read from channel_read() so we check EOF status to avoid getting stuck in a loop */ if(libssh2_channel_eof(session->scpSend_channel)) { - _libssh2_error(session, LIBSSH2_ERROR_SCP_PROTOCOL, - "Unexpected channel close"); + ssh2_err(session, LIBSSH2_ERROR_SCP_PROTOCOL, + "Unexpected channel close"); } else return session->scpSend_channel; @@ -1137,7 +1120,7 @@ scp_send_error: session->err_code = tmp_err_code; session->err_msg = tmp_err_msg; session->scpSend_channel = NULL; - session->scpSend_state = libssh2_NB_state_idle; + session->scpSend_state = ssh2_NB_state_idle; return NULL; } diff --git a/src/session.c b/src/session.c index 4681c09c..24d1c634 100644 --- a/src/session.c +++ b/src/session.c @@ -68,22 +68,25 @@ #undef libssh2_usec_t #endif -/* libssh2_default_alloc */ -static LIBSSH2_ALLOC_FUNC(libssh2_default_alloc) +#define SSH2_DEFAULT_READ_TIMEOUT 60 /* generic timeout in seconds used when + waiting for more data to arrive */ + +/* ssh2_default_alloc */ +static LIBSSH2_ALLOC_FUNC(ssh2_default_alloc) { (void)abstract; return malloc(count); } -/* libssh2_default_free */ -static LIBSSH2_FREE_FUNC(libssh2_default_free) +/* ssh2_default_free */ +static LIBSSH2_FREE_FUNC(ssh2_default_free) { (void)abstract; free(ptr); } -/* libssh2_default_realloc */ -static LIBSSH2_REALLOC_FUNC(libssh2_default_realloc) +/* ssh2_default_realloc */ +static LIBSSH2_REALLOC_FUNC(ssh2_default_realloc) { (void)abstract; return realloc(ptr, count); @@ -100,10 +103,10 @@ static int banner_receive(LIBSSH2_SESSION *session) ssize_t ret; size_t banner_len; - if(session->banner_TxRx_state == libssh2_NB_state_idle) { + if(session->banner_TxRx_state == ssh2_NB_state_idle) { banner_len = 0; - session->banner_TxRx_state = libssh2_NB_state_created; + session->banner_TxRx_state = ssh2_NB_state_created; } else { banner_len = session->banner_TxRx_total_send; @@ -117,16 +120,16 @@ static int banner_receive(LIBSSH2_SESSION *session) /* no incoming block yet! */ session->socket_block_directions &= ~LIBSSH2_SESSION_BLOCK_INBOUND; - ret = LIBSSH2_RECV(session, &c, 1, LIBSSH2_SOCKET_RECV_FLAGS(session)); + ret = SSH2_RECV(session, &c, 1, SSH2_SOCKET_RECV_FLAGS(session)); if(ret < 0) { if(session->api_block_mode || (ret != -EAGAIN)) /* ignore EAGAIN when non-blocking */ - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Error recving %d bytes: %ld", 1, (long)-ret)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Error recving %d bytes: %ld", 1, (long)-ret)); } else - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Recved %ld bytes banner", (long)ret)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, "Recved %ld bytes banner", + (long)ret)); if(ret < 0) { if(ret == -EAGAIN) { @@ -137,13 +140,13 @@ static int banner_receive(LIBSSH2_SESSION *session) } /* Some kinda error */ - session->banner_TxRx_state = libssh2_NB_state_idle; + session->banner_TxRx_state = ssh2_NB_state_idle; session->banner_TxRx_total_send = 0; return LIBSSH2_ERROR_SOCKET_RECV; } if(ret == 0) { - session->socket_state = LIBSSH2_SOCKET_DISCONNECTED; + session->socket_state = SSH2_SOCKET_DISCONNECTED; return LIBSSH2_ERROR_SOCKET_DISCONNECT; } @@ -153,7 +156,7 @@ static int banner_receive(LIBSSH2_SESSION *session) if(c == '\0') { /* NULLs are not allowed in SSH banners */ - session->banner_TxRx_state = libssh2_NB_state_idle; + session->banner_TxRx_state = ssh2_NB_state_idle; session->banner_TxRx_total_send = 0; return LIBSSH2_ERROR_BANNER_RECV; } @@ -168,29 +171,29 @@ static int banner_receive(LIBSSH2_SESSION *session) } /* From this point on, we are done here */ - session->banner_TxRx_state = libssh2_NB_state_idle; + session->banner_TxRx_state = ssh2_NB_state_idle; session->banner_TxRx_total_send = 0; if(!banner_len) return LIBSSH2_ERROR_BANNER_RECV; if(session->remote.banner) - LIBSSH2_FREE(session, session->remote.banner); + SSH2_FREE(session, session->remote.banner); - session->remote.banner = LIBSSH2_ALLOC(session, banner_len + 1); + session->remote.banner = SSH2_ALLOC(session, banner_len + 1); if(!session->remote.banner) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Error allocating space for remote banner"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Error allocating space for remote banner"); } memcpy(session->remote.banner, session->banner_TxRx_banner, banner_len); session->remote.banner[banner_len] = '\0'; - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, "Received Banner: %s", - session->remote.banner)); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Received Banner: %s", + session->remote.banner)); return LIBSSH2_ERROR_NONE; } /* - * Send the default banner, or the one set via libssh2_setopt_string + * Send the default banner, or the one set via libssh2_session_banner_set() * * Returns LIBSSH2_ERROR_EAGAIN if it would block - and if it does so, you * should call this function again as soon as it is likely that more data can @@ -203,7 +206,7 @@ static int banner_send(LIBSSH2_SESSION *session) size_t banner_len = sizeof(LIBSSH2_SSH_DEFAULT_BANNER_WITH_CRLF) - 1; ssize_t ret; - if(session->banner_TxRx_state == libssh2_NB_state_idle) { + if(session->banner_TxRx_state == ssh2_NB_state_idle) { if(session->local.banner) { /* setopt_string has given us our \r\n characters */ banner_len = strlen((char *)session->local.banner); @@ -223,32 +226,31 @@ static int banner_send(LIBSSH2_SESSION *session) banner_dup[255] = '\0'; } - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Sending Banner: %s", banner_dup)); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Sending Banner: %s", + banner_dup)); } #endif - session->banner_TxRx_state = libssh2_NB_state_created; + session->banner_TxRx_state = ssh2_NB_state_created; } /* no outgoing block yet! */ session->socket_block_directions &= ~LIBSSH2_SESSION_BLOCK_OUTBOUND; - ret = LIBSSH2_SEND(session, - banner + session->banner_TxRx_total_send, - banner_len - session->banner_TxRx_total_send, - LIBSSH2_SOCKET_SEND_FLAGS(session)); + ret = SSH2_SEND(session, banner + session->banner_TxRx_total_send, + banner_len - session->banner_TxRx_total_send, + SSH2_SOCKET_SEND_FLAGS(session)); if(ret < 0) - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Error sending %ld bytes: %ld", - (long)(banner_len - session->banner_TxRx_total_send), - (long)-ret)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Error sending %ld bytes: %ld", + (long)(banner_len - session->banner_TxRx_total_send), + (long)-ret)); else - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Sent %ld/%ld bytes at %p+%ld", (long)ret, - (long)(banner_len - session->banner_TxRx_total_send), - (const void *)banner, - (long)session->banner_TxRx_total_send)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Sent %ld/%ld bytes at %p+%ld", (long)ret, + (long)(banner_len - session->banner_TxRx_total_send), + (const void *)banner, + (long)session->banner_TxRx_total_send)); if(ret != (ssize_t)(banner_len - session->banner_TxRx_total_send)) { if(ret >= 0 || ret == -EAGAIN) { @@ -258,13 +260,13 @@ static int banner_send(LIBSSH2_SESSION *session) session->banner_TxRx_total_send += ret; return LIBSSH2_ERROR_EAGAIN; } - session->banner_TxRx_state = libssh2_NB_state_idle; + session->banner_TxRx_state = ssh2_NB_state_idle; session->banner_TxRx_total_send = 0; return LIBSSH2_ERROR_SOCKET_RECV; } /* Set the state back to idle */ - session->banner_TxRx_state = libssh2_NB_state_idle; + session->banner_TxRx_state = ssh2_NB_state_idle; session->banner_TxRx_total_send = 0; return 0; @@ -375,25 +377,25 @@ int libssh2_session_banner_set(LIBSSH2_SESSION *session, const char *banner) size_t banner_len = banner ? strlen(banner) : 0; if(session->local.banner) { - LIBSSH2_FREE(session, session->local.banner); + SSH2_FREE(session, session->local.banner); session->local.banner = NULL; } if(!banner_len) return 0; - session->local.banner = LIBSSH2_ALLOC(session, banner_len + 3); + session->local.banner = SSH2_ALLOC(session, banner_len + 3); if(!session->local.banner) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for local banner"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for local banner"); } memcpy(session->local.banner, banner, banner_len); /* first null-terminate like this so that the debug output is nice */ session->local.banner[banner_len] = '\0'; - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, "Setting local Banner: %s", - session->local.banner)); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Setting local Banner: %s", + session->local.banner)); session->local.banner[banner_len++] = '\r'; session->local.banner[banner_len++] = '\n'; session->local.banner[banner_len] = '\0'; @@ -425,9 +427,9 @@ LIBSSH2_SESSION *libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC(*my_alloc), LIBSSH2_REALLOC_FUNC(*my_realloc), void *abstract) { - LIBSSH2_ALLOC_FUNC(*local_alloc) = libssh2_default_alloc; - LIBSSH2_FREE_FUNC(*local_free) = libssh2_default_free; - LIBSSH2_REALLOC_FUNC(*local_realloc) = libssh2_default_realloc; + LIBSSH2_ALLOC_FUNC(*local_alloc) = ssh2_default_alloc; + LIBSSH2_FREE_FUNC(*local_free) = ssh2_default_free; + LIBSSH2_REALLOC_FUNC(*local_realloc) = ssh2_default_realloc; LIBSSH2_SESSION *session; if(my_alloc) { @@ -446,20 +448,20 @@ LIBSSH2_SESSION *libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC(*my_alloc), session->alloc = local_alloc; session->free = local_free; session->realloc = local_realloc; - session->send = _libssh2_send; - session->recv = _libssh2_recv; + session->send = ssh2_send; + session->recv = ssh2_recv; session->abstract = abstract; session->api_timeout = 0; /* timeout-free API by default */ session->api_block_mode = 1; /* blocking API by default */ - session->state = LIBSSH2_STATE_INITIAL_KEX; + session->state = SSH2_STATE_INITIAL_KEX; session->fullpacket_required_type = 0; - session->packet_read_timeout = LIBSSH2_DEFAULT_READ_TIMEOUT; + session->packet_read_timeout = SSH2_DEFAULT_READ_TIMEOUT; session->flag.quote_paths = 1; /* default behavior is to quote paths for the scp subsystem */ session->kex = NULL; - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "New session resource allocated")); - _libssh2_init_if_needed(); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "New session resource allocated")); + ssh2_init_if_needed(); } return session; } @@ -530,8 +532,7 @@ libssh2_cb_generic *libssh2_session_callback_set2(LIBSSH2_SESSION *session, session->agentSignCallback = (LIBSSH2_AUTHAGENT_SIGN_FUNC(*))callback; return oldcb; } - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, "Setting Callback %d", - cbtype)); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Setting Callback %d", cbtype)); return NULL; } @@ -575,7 +576,7 @@ void *libssh2_session_callback_set(LIBSSH2_SESSION *session, * Utility function that waits for action on the socket. Returns 0 when ready * to run again or error on timeout. */ -int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time) +int ssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time) { int rc; int seconds_to_next; @@ -600,8 +601,8 @@ int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time) dir = libssh2_session_block_directions(session); if(!dir) { - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Nothing to wait for in wait_socket")); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Nothing to wait for in wait_socket")); /* To avoid that we hang below because there is nothing set to wait for, we timeout on 1 second to also avoid busy-looping during this condition */ @@ -613,8 +614,8 @@ int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time) time_t now = time(NULL); elapsed_ms = (long)(1000 * difftime(now, start_time)); if(elapsed_ms > session->api_timeout) { - return _libssh2_error(session, LIBSSH2_ERROR_TIMEOUT, - "API timeout expired"); + return ssh2_err(session, LIBSSH2_ERROR_TIMEOUT, + "API timeout expired"); } ms_to_next = (session->api_timeout - elapsed_ms); has_timeout = 1; @@ -687,13 +688,13 @@ int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time) } #endif if(rc == 0) { - return _libssh2_error(session, LIBSSH2_ERROR_TIMEOUT, - "Timed out waiting on socket"); + return ssh2_err(session, LIBSSH2_ERROR_TIMEOUT, + "Timed out waiting on socket"); } if(rc < 0) { int err; #ifdef _WIN32 - err = _libssh2_wsa2errno(); + err = ssh2_wsa2errno(); #else err = errno; #endif @@ -703,8 +704,8 @@ int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time) if(err == EINTR) return 0; - return _libssh2_error(session, LIBSSH2_ERROR_TIMEOUT, - "Error waiting on socket"); + return ssh2_err(session, LIBSSH2_ERROR_TIMEOUT, + "Error waiting on socket"); } return 0; /* ready to try again */ @@ -719,13 +720,13 @@ static int session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock) return LIBSSH2_ERROR_PROTO; } - if(session->startup_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "session_startup for socket %ld", (long)sock)); + if(session->startup_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "session_startup for socket %ld", (long)sock)); if(LIBSSH2_INVALID_SOCKET == sock) { /* Did we forget something? */ - return _libssh2_error(session, LIBSSH2_ERROR_BAD_SOCKET, - "Bad socket provided"); + return ssh2_err(session, LIBSSH2_ERROR_BAD_SOCKET, + "Bad socket provided"); } session->socket_fd = sock; @@ -736,86 +737,84 @@ static int session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock) /* If in blocking state change to non-blocking */ rc = session_nonblock(session->socket_fd, 1); if(rc) { - return _libssh2_error(session, rc, - "Failed changing socket's " - "blocking state to non-blocking"); + return ssh2_err(session, rc, + "Failed changing socket's " + "blocking state to non-blocking"); } } - session->startup_state = libssh2_NB_state_created; + session->startup_state = ssh2_NB_state_created; } - if(session->startup_state == libssh2_NB_state_created) { + if(session->startup_state == ssh2_NB_state_created) { rc = banner_send(session); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; else if(rc) { - return _libssh2_error(session, rc, "Failed sending banner"); + return ssh2_err(session, rc, "Failed sending banner"); } - session->startup_state = libssh2_NB_state_sent; - session->banner_TxRx_state = libssh2_NB_state_idle; + session->startup_state = ssh2_NB_state_sent; + session->banner_TxRx_state = ssh2_NB_state_idle; } - if(session->startup_state == libssh2_NB_state_sent) { + if(session->startup_state == ssh2_NB_state_sent) { do { rc = banner_receive(session); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; else if(rc) - return _libssh2_error(session, rc, "Failed getting banner"); + return ssh2_err(session, rc, "Failed getting banner"); } while(strncmp("SSH-", (const char *)session->remote.banner, 4)); - session->startup_state = libssh2_NB_state_sent1; + session->startup_state = ssh2_NB_state_sent1; } - if(session->startup_state == libssh2_NB_state_sent1) { - rc = _libssh2_kex_exchange(session, 0, &session->startup_key_state); + if(session->startup_state == ssh2_NB_state_sent1) { + rc = ssh2_kex_exchange(session, 0, &session->startup_key_state); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; else if(rc) - return _libssh2_error(session, rc, - "Unable to exchange encryption keys"); + return ssh2_err(session, rc, "Unable to exchange encryption keys"); - session->startup_state = libssh2_NB_state_sent2; + session->startup_state = ssh2_NB_state_sent2; } - if(session->startup_state == libssh2_NB_state_sent2) { - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Requesting userauth service")); + if(session->startup_state == ssh2_NB_state_sent2) { + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "Requesting userauth service")); /* Request the userauth service */ session->startup_service[0] = SSH_MSG_SERVICE_REQUEST; - _libssh2_htonu32(session->startup_service + 1, - sizeof("ssh-userauth") - 1); + ssh2_htonu32(session->startup_service + 1, sizeof("ssh-userauth") - 1); memcpy(session->startup_service + 5, "ssh-userauth", sizeof("ssh-userauth") - 1); - session->startup_state = libssh2_NB_state_sent3; + session->startup_state = ssh2_NB_state_sent3; } - if(session->startup_state == libssh2_NB_state_sent3) { - rc = _libssh2_transport_send(session, session->startup_service, - sizeof("ssh-userauth") + 5 - 1, NULL, 0); + if(session->startup_state == ssh2_NB_state_sent3) { + rc = ssh2_transport_send(session, session->startup_service, + sizeof("ssh-userauth") + 5 - 1, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; else if(rc) { - return _libssh2_error(session, rc, - "Unable to ask for ssh-userauth service"); + return ssh2_err(session, rc, + "Unable to ask for ssh-userauth service"); } - session->startup_state = libssh2_NB_state_sent4; + session->startup_state = ssh2_NB_state_sent4; } - if(session->startup_state == libssh2_NB_state_sent4) { + if(session->startup_state == ssh2_NB_state_sent4) { struct string_buf buf; - rc = _libssh2_packet_require(session, SSH_MSG_SERVICE_ACCEPT, - &session->startup_data, - &session->startup_data_len, 0, NULL, 0, - &session->startup_req_state); + rc = ssh2_packet_require(session, SSH_MSG_SERVICE_ACCEPT, + &session->startup_data, + &session->startup_data_len, 0, NULL, 0, + &session->startup_req_state); if(rc) - return _libssh2_error(session, rc, - "Failed to get response to " - "ssh-userauth request"); + return ssh2_err(session, rc, + "Failed to get response to " + "ssh-userauth request"); buf.data = session->startup_data; buf.dataptr = buf.data; @@ -824,19 +823,19 @@ static int session_startup(LIBSSH2_SESSION *session, libssh2_socket_t sock) /* advance past packet type */ buf.dataptr++; - if(_libssh2_match_string(&buf, "ssh-userauth")) { - LIBSSH2_FREE(session, session->startup_data); + if(ssh2_match_string(&buf, "ssh-userauth")) { + SSH2_FREE(session, session->startup_data); session->startup_data = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Invalid response received from server"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Invalid response received from server"); } session->startup_service_length = (sizeof("ssh-userauth") - 1); - LIBSSH2_FREE(session, session->startup_data); + SSH2_FREE(session, session->startup_data); session->startup_data = NULL; - session->startup_state = libssh2_NB_state_idle; + session->startup_state = ssh2_NB_state_idle; return 0; } @@ -890,34 +889,33 @@ static int session_free(LIBSSH2_SESSION *session) LIBSSH2_LISTENER *l; int packets_left = 0; - if(session->free_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Freeing session resource %p", - (void *)session->remote.banner)); + if(session->free_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Freeing session resource %p", + (void *)session->remote.banner)); - session->free_state = libssh2_NB_state_created; + session->free_state = ssh2_NB_state_created; } - if(session->free_state == libssh2_NB_state_created) { + if(session->free_state == ssh2_NB_state_created) { /* !checksrc! disable EQUALSNULL 1 */ - while((ch = _libssh2_list_first(&session->channels)) != NULL) { - rc = _libssh2_channel_free(ch); + while((ch = ssh2_list_first(&session->channels)) != NULL) { + rc = ssh2_channel_free(ch); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; } - session->free_state = libssh2_NB_state_sent; + session->free_state = ssh2_NB_state_sent; } - if(session->free_state == libssh2_NB_state_sent) { + if(session->free_state == ssh2_NB_state_sent) { /* !checksrc! disable EQUALSNULL 1 */ - while((l = _libssh2_list_first(&session->listeners)) != NULL) { - rc = _libssh2_channel_forward_cancel(l); + while((l = ssh2_list_first(&session->listeners)) != NULL) { + rc = ssh2_channel_forward_cancel(l); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; } - session->free_state = libssh2_NB_state_sent1; + session->free_state = ssh2_NB_state_sent1; } if(session->kex && session->kex->cleanup) { @@ -925,7 +923,7 @@ static int session_free(LIBSSH2_SESSION *session) &session->startup_key_state.key_state_low); } - if(session->state & LIBSSH2_STATE_NEWKEYS) { + if(session->state & SSH2_STATE_NEWKEYS) { /* hostkey */ if(session->hostkey && session->hostkey->dtor) { session->hostkey->dtor(session, &session->server_hostkey_abstract); @@ -965,179 +963,179 @@ static int session_free(LIBSSH2_SESSION *session) /* session_id */ if(session->session_id) { - LIBSSH2_FREE(session, session->session_id); + SSH2_FREE(session, session->session_id); } } /* Free banner(s) */ if(session->remote.banner) { - LIBSSH2_FREE(session, session->remote.banner); + SSH2_FREE(session, session->remote.banner); } if(session->local.banner) { - LIBSSH2_FREE(session, session->local.banner); + SSH2_FREE(session, session->local.banner); } /* Free preference(s) */ if(session->kex_prefs) { - LIBSSH2_FREE(session, session->kex_prefs); + SSH2_FREE(session, session->kex_prefs); } if(session->hostkey_prefs) { - LIBSSH2_FREE(session, session->hostkey_prefs); + SSH2_FREE(session, session->hostkey_prefs); } if(session->local.kexinit) { - LIBSSH2_FREE(session, session->local.kexinit); + SSH2_FREE(session, session->local.kexinit); } if(session->local.crypt_prefs) { - LIBSSH2_FREE(session, session->local.crypt_prefs); + SSH2_FREE(session, session->local.crypt_prefs); } if(session->local.mac_prefs) { - LIBSSH2_FREE(session, session->local.mac_prefs); + SSH2_FREE(session, session->local.mac_prefs); } if(session->local.comp_prefs) { - LIBSSH2_FREE(session, session->local.comp_prefs); + SSH2_FREE(session, session->local.comp_prefs); } if(session->local.lang_prefs) { - LIBSSH2_FREE(session, session->local.lang_prefs); + SSH2_FREE(session, session->local.lang_prefs); } if(session->remote.kexinit) { - LIBSSH2_FREE(session, session->remote.kexinit); + SSH2_FREE(session, session->remote.kexinit); } if(session->remote.crypt_prefs) { - LIBSSH2_FREE(session, session->remote.crypt_prefs); + SSH2_FREE(session, session->remote.crypt_prefs); } if(session->remote.mac_prefs) { - LIBSSH2_FREE(session, session->remote.mac_prefs); + SSH2_FREE(session, session->remote.mac_prefs); } if(session->remote.comp_prefs) { - LIBSSH2_FREE(session, session->remote.comp_prefs); + SSH2_FREE(session, session->remote.comp_prefs); } if(session->remote.lang_prefs) { - LIBSSH2_FREE(session, session->remote.lang_prefs); + SSH2_FREE(session, session->remote.lang_prefs); } if(session->server_sign_algorithms) { - LIBSSH2_FREE(session, session->server_sign_algorithms); + SSH2_FREE(session, session->server_sign_algorithms); } if(session->sign_algo_prefs) { - LIBSSH2_FREE(session, session->sign_algo_prefs); + SSH2_FREE(session, session->sign_algo_prefs); } /* * Make sure all memory used in the state variables are free */ if(session->kexinit_data) { - LIBSSH2_FREE(session, session->kexinit_data); + SSH2_FREE(session, session->kexinit_data); } if(session->startup_data) { - LIBSSH2_FREE(session, session->startup_data); + SSH2_FREE(session, session->startup_data); } if(session->userauth_list_data) { - LIBSSH2_FREE(session, session->userauth_list_data); + SSH2_FREE(session, session->userauth_list_data); } if(session->userauth_banner) { - LIBSSH2_FREE(session, session->userauth_banner); + SSH2_FREE(session, session->userauth_banner); } if(session->userauth_pswd_data) { - LIBSSH2_FREE(session, session->userauth_pswd_data); + SSH2_FREE(session, session->userauth_pswd_data); } if(session->userauth_pswd_newpw) { - LIBSSH2_FREE(session, session->userauth_pswd_newpw); + SSH2_FREE(session, session->userauth_pswd_newpw); } if(session->userauth_host_packet) { - LIBSSH2_FREE(session, session->userauth_host_packet); + SSH2_FREE(session, session->userauth_host_packet); } if(session->userauth_host_method) { - LIBSSH2_FREE(session, session->userauth_host_method); + SSH2_FREE(session, session->userauth_host_method); } if(session->userauth_host_data) { - LIBSSH2_FREE(session, session->userauth_host_data); + SSH2_FREE(session, session->userauth_host_data); } if(session->userauth_pblc_data) { - LIBSSH2_FREE(session, session->userauth_pblc_data); + SSH2_FREE(session, session->userauth_pblc_data); } if(session->userauth_pblc_packet) { - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); } if(session->userauth_pblc_method) { - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); } if(session->userauth_kybd_data) { - LIBSSH2_FREE(session, session->userauth_kybd_data); + SSH2_FREE(session, session->userauth_kybd_data); } if(session->userauth_kybd_packet) { - LIBSSH2_FREE(session, session->userauth_kybd_packet); + SSH2_FREE(session, session->userauth_kybd_packet); } if(session->userauth_kybd_auth_instruction) { - LIBSSH2_FREE(session, session->userauth_kybd_auth_instruction); + SSH2_FREE(session, session->userauth_kybd_auth_instruction); } if(session->open_packet) { - LIBSSH2_FREE(session, session->open_packet); + SSH2_FREE(session, session->open_packet); } if(session->open_data) { - LIBSSH2_FREE(session, session->open_data); + SSH2_FREE(session, session->open_data); } if(session->direct_message) { - LIBSSH2_FREE(session, session->direct_message); + SSH2_FREE(session, session->direct_message); } if(session->fwdLstn_packet) { - LIBSSH2_FREE(session, session->fwdLstn_packet); + SSH2_FREE(session, session->fwdLstn_packet); } if(session->pkeyInit_data) { - LIBSSH2_FREE(session, session->pkeyInit_data); + SSH2_FREE(session, session->pkeyInit_data); } if(session->scpRecv_command) { - LIBSSH2_FREE(session, session->scpRecv_command); + SSH2_FREE(session, session->scpRecv_command); } if(session->scpSend_command) { - LIBSSH2_FREE(session, session->scpSend_command); + SSH2_FREE(session, session->scpSend_command); } if(session->sftpInit_sftp) { - LIBSSH2_FREE(session, session->sftpInit_sftp); + SSH2_FREE(session, session->sftpInit_sftp); } /* Free payload buffer */ if(session->packet.total_num) { - LIBSSH2_FREE(session, session->packet.payload); + SSH2_FREE(session, session->packet.payload); } /* Cleanup all remaining packets */ /* !checksrc! disable EQUALSNULL 1 */ - while((pkg = _libssh2_list_first(&session->packets)) != NULL) { + while((pkg = ssh2_list_first(&session->packets)) != NULL) { packets_left++; - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "packet left with id %d", pkg->data[0])); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "packet left with id %d", + pkg->data[0])); /* unlink the node */ - _libssh2_list_remove(&pkg->node); + ssh2_list_remove(&pkg->node); /* free */ - LIBSSH2_FREE(session, pkg->data); - LIBSSH2_FREE(session, pkg); + SSH2_FREE(session, pkg->data); + SSH2_FREE(session, pkg); } (void)packets_left; - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Extra packets left %d", packets_left)); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Extra packets left %d", + packets_left)); if(session->socket_prev_blockstate) { /* if the socket was previously blocking, put it back so */ rc = session_nonblock(session->socket_fd, 0); if(rc) { - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "unable to reset socket's blocking state")); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "unable to reset socket's blocking state")); } } if(session->server_hostkey) { - LIBSSH2_FREE(session, session->server_hostkey); + SSH2_FREE(session, session->server_hostkey); } /* error string */ if(session->err_msg && - ((session->err_flags & LIBSSH2_ERR_FLAG_DUP) != 0)) { - LIBSSH2_FREE(session, (char *)LIBSSH2_UNCONST(session->err_msg)); + ((session->err_flags & SSH2_ERR_FLAG_DUP) != 0)) { + SSH2_FREE(session, (char *)SSH2_UNCONST(session->err_msg)); } - LIBSSH2_FREE(session, session); + SSH2_FREE(session, session); return 0; } @@ -1164,10 +1162,10 @@ static int session_disconnect(LIBSSH2_SESSION *session, int reason, size_t descr_len = 0, lang_len = 0; int rc; - if(session->disconnect_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, - "Disconnecting: reason=%d, desc=%s, lang=%s", reason, - description, lang)); + if(session->disconnect_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_TRANS, + "Disconnecting: reason=%d, desc=%s, lang=%s", + reason, description, lang)); if(description) descr_len = strlen(description); @@ -1175,12 +1173,12 @@ static int session_disconnect(LIBSSH2_SESSION *session, int reason, lang_len = strlen(lang); if(descr_len > 256) - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "too long description"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "too long description"); if(lang_len > 256) - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "too long language string"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "too long language string"); /* 13 = packet_type(1) + reason code(4) + descr_len(4) + lang_len(4) */ session->disconnect_data_len = descr_len + lang_len + 13; @@ -1188,21 +1186,21 @@ static int session_disconnect(LIBSSH2_SESSION *session, int reason, s = session->disconnect_data; *(s++) = SSH_MSG_DISCONNECT; - _libssh2_store_u32(&s, reason); - _libssh2_store_str(&s, description, descr_len); + ssh2_store_u32(&s, reason); + ssh2_store_str(&s, description, descr_len); /* store length only, lang is sent separately */ - _libssh2_store_u32(&s, (uint32_t)lang_len); + ssh2_store_u32(&s, (uint32_t)lang_len); - session->disconnect_state = libssh2_NB_state_created; + session->disconnect_state = ssh2_NB_state_created; } - rc = _libssh2_transport_send(session, session->disconnect_data, - session->disconnect_data_len, - (const unsigned char *)lang, lang_len); + rc = ssh2_transport_send(session, session->disconnect_data, + session->disconnect_data_len, + (const unsigned char *)lang, lang_len); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; - session->disconnect_state = libssh2_NB_state_idle; + session->disconnect_state = ssh2_NB_state_idle; return 0; } @@ -1212,8 +1210,8 @@ int libssh2_session_disconnect_ex(LIBSSH2_SESSION *session, int reason, const char *description, const char *lang) { int rc; - session->state &= ~LIBSSH2_STATE_INITIAL_KEX; - session->state &= ~LIBSSH2_STATE_EXCHANGING_KEYS; + session->state &= ~SSH2_STATE_INITIAL_KEX; + session->state &= ~SSH2_STATE_EXCHANGING_KEYS; BLOCK_ADJUST(rc, session, session_disconnect(session, reason, description, lang)); @@ -1272,14 +1270,13 @@ const char *libssh2_session_methods(LIBSSH2_SESSION *session, int method_type) return ""; default: - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Invalid parameter specified for method_type"); + ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Invalid parameter specified for method_type"); return NULL; } if(!method) { - _libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE, - "No method negotiated"); + ssh2_err(session, LIBSSH2_ERROR_METHOD_NONE, "No method negotiated"); return NULL; } @@ -1310,13 +1307,13 @@ int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg, if(!session->err_code) { if(errmsg) { if(want_buf) { - *errmsg = LIBSSH2_ALLOC(session, 1); + *errmsg = SSH2_ALLOC(session, 1); if(*errmsg) { **errmsg = 0; } } else { - *errmsg = (char *)LIBSSH2_UNCONST(""); + *errmsg = (char *)SSH2_UNCONST(""); } } if(errmsg_len) { @@ -1332,14 +1329,14 @@ int libssh2_session_last_error(LIBSSH2_SESSION *session, char **errmsg, if(want_buf) { /* Make a copy so the calling program can own it */ - *errmsg = LIBSSH2_ALLOC(session, msglen + 1); + *errmsg = SSH2_ALLOC(session, msglen + 1); if(*errmsg) { memcpy(*errmsg, error, msglen); (*errmsg)[msglen] = 0; } } else - *errmsg = (char *)LIBSSH2_UNCONST(error); + *errmsg = (char *)SSH2_UNCONST(error); } if(errmsg_len) { @@ -1367,11 +1364,9 @@ int libssh2_session_last_errno(LIBSSH2_SESSION *session) */ LIBSSH2_API int libssh2_session_set_last_error(LIBSSH2_SESSION *session, - int errcode, - const char *errmsg) + int errcode, const char *errmsg) { - return _libssh2_error_flags(session, errcode, errmsg, - LIBSSH2_ERR_FLAG_DUP); + return ssh2_err_flags(session, errcode, errmsg, SSH2_ERR_FLAG_DUP); } /* @@ -1405,11 +1400,11 @@ int libssh2_session_flag(LIBSSH2_SESSION *session, int flag, int value) * this function is called. Note this function does not alter the state of the * actual socket involved. */ -int _libssh2_session_set_blocking(LIBSSH2_SESSION *session, int blocking) +int ssh2_session_set_blocking(LIBSSH2_SESSION *session, int blocking) { int bl = session->api_block_mode; - _libssh2_debug((session, LIBSSH2_TRACE_CONN, - "Setting blocking mode %s", blocking ? "ON" : "OFF")); + ssh2_deb((session, LIBSSH2_TRACE_CONN, "Setting blocking mode %s", + blocking ? "ON" : "OFF")); session->api_block_mode = blocking; return bl; @@ -1422,7 +1417,7 @@ int _libssh2_session_set_blocking(LIBSSH2_SESSION *session, int blocking) LIBSSH2_API void libssh2_session_set_blocking(LIBSSH2_SESSION *session, int blocking) { - (void)_libssh2_session_set_blocking(session, blocking); + (void)ssh2_session_set_blocking(session, blocking); } /* @@ -1461,7 +1456,7 @@ LIBSSH2_API void libssh2_session_set_read_timeout(LIBSSH2_SESSION *session, long timeout) { if(timeout <= 0) { - timeout = LIBSSH2_DEFAULT_READ_TIMEOUT; + timeout = SSH2_DEFAULT_READ_TIMEOUT; } session->packet_read_timeout = timeout; } @@ -1489,15 +1484,15 @@ int libssh2_poll_channel_read(LIBSSH2_CHANNEL *channel, int extended) return LIBSSH2_ERROR_BAD_USE; session = channel->session; - packet = _libssh2_list_first(&session->packets); + packet = ssh2_list_first(&session->packets); while(packet) { if(packet->data_len < 5) { - return _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Packet too small"); + return ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Packet too small"); } - if(channel->local.id == _libssh2_ntohu32(packet->data + 1)) { + if(channel->local.id == ssh2_ntohu32(packet->data + 1)) { if(extended == 1 && (packet->data[0] == SSH_MSG_CHANNEL_EXTENDED_DATA || packet->data[0] == SSH_MSG_CHANNEL_DATA)) { @@ -1509,7 +1504,7 @@ int libssh2_poll_channel_read(LIBSSH2_CHANNEL *channel, int extended) } /* else - no data of any type is ready to be read */ } - packet = _libssh2_list_next(&packet->node); + packet = ssh2_list_next(&packet->node); } return 0; @@ -1530,7 +1525,7 @@ static inline int poll_channel_write(LIBSSH2_CHANNEL *channel) */ static inline int poll_listener_queued(LIBSSH2_LISTENER *listener) { - return _libssh2_list_first(&listener->queue) ? 1 : 0; + return ssh2_list_first(&listener->queue) ? 1 : 0; } #ifndef LIBSSH2_NO_DEPRECATED @@ -1582,8 +1577,8 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) default: if(session) - _libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE, - "Invalid descriptor passed to libssh2_poll()"); + ssh2_err(session, LIBSSH2_ERROR_INVALID_POLL_TYPE, + "Invalid descriptor passed to libssh2_poll()"); return -1; } } @@ -1657,8 +1652,8 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) default: if(session) - _libssh2_error(session, LIBSSH2_ERROR_INVALID_POLL_TYPE, - "Invalid descriptor passed to libssh2_poll()"); + ssh2_err(session, LIBSSH2_ERROR_INVALID_POLL_TYPE, + "Invalid descriptor passed to libssh2_poll()"); return -1; } } @@ -1709,7 +1704,7 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) fds[i].revents |= LIBSSH2_POLLFD_CHANNEL_CLOSED; } if(fds[i].fd.channel->session->socket_state == - LIBSSH2_SOCKET_DISCONNECTED) { + SSH2_SOCKET_DISCONNECTED) { fds[i].revents |= LIBSSH2_POLLFD_CHANNEL_CLOSED | LIBSSH2_POLLFD_SESSION_CLOSED; @@ -1726,7 +1721,7 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) LIBSSH2_POLLFD_POLLIN : 0; } if(fds[i].fd.listener->session->socket_state == - LIBSSH2_SOCKET_DISCONNECTED) { + SSH2_SOCKET_DISCONNECTED) { fds[i].revents |= LIBSSH2_POLLFD_LISTENER_CLOSED | LIBSSH2_POLLFD_SESSION_CLOSED; @@ -1770,7 +1765,7 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) case LIBSSH2_POLLFD_CHANNEL: if(sockets[i].events & POLLIN) { /* Spin session until no data available */ - while(_libssh2_transport_read(fds[i].fd. + while(ssh2_transport_read(fds[i].fd. channel->session) > 0); } @@ -1784,7 +1779,7 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) case LIBSSH2_POLLFD_LISTENER: if(sockets[i].events & POLLIN) { /* Spin session until no data available */ - while(_libssh2_transport_read(fds[i].fd. + while(ssh2_transport_read(fds[i].fd. listener->session) > 0); } @@ -1843,7 +1838,7 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) if(FD_ISSET(fds[i].fd.channel->session->socket_fd, &rfds)) { /* Spin session until no data available */ - while(_libssh2_transport_read(fds[i].fd. + while(ssh2_transport_read(fds[i].fd. channel->session) > 0); } @@ -1853,7 +1848,7 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) if(FD_ISSET(fds[i].fd.listener->session->socket_fd, &rfds)) { /* Spin session until no data available */ - while(_libssh2_transport_read(fds[i].fd. + while(ssh2_transport_read(fds[i].fd. listener->session) > 0); } @@ -1871,8 +1866,8 @@ int libssh2_poll(LIBSSH2_POLLFD *fds, unsigned int nfds, long timeout) /* * Get blocked direction when a function returns LIBSSH2_ERROR_EAGAIN - * Returns LIBSSH2_SOCKET_BLOCK_INBOUND if recv() blocked - * or LIBSSH2_SOCKET_BLOCK_OUTBOUND if send() blocked + * Returns SSH2_SOCKET_BLOCK_INBOUND if recv() blocked + * or SSH2_SOCKET_BLOCK_OUTBOUND if send() blocked */ LIBSSH2_API int libssh2_session_block_directions(LIBSSH2_SESSION *session) diff --git a/src/session.h b/src/session.h index 5a1da60e..01fc405f 100644 --- a/src/session.h +++ b/src/session.h @@ -62,7 +62,7 @@ if(((rc) != LIBSSH2_ERROR_EAGAIN) || !(sess) || \ !(sess)->api_block_mode) \ break; \ - (rc) = _libssh2_wait_socket(sess, entry_time); \ + (rc) = ssh2_wait_socket(sess, entry_time); \ } while(!(rc)); \ } while(0) @@ -81,13 +81,13 @@ if(!(sess) || !(sess)->api_block_mode || (ptr) != NULL || \ libssh2_session_last_errno(sess) != LIBSSH2_ERROR_EAGAIN) \ break; \ - (rc) = _libssh2_wait_socket(sess, entry_time); \ + (rc) = ssh2_wait_socket(sess, entry_time); \ } while(!(rc)); \ } while(0) -int _libssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time); +int ssh2_wait_socket(LIBSSH2_SESSION *session, time_t start_time); /* this is the lib-internal set blocking function */ -int _libssh2_session_set_blocking(LIBSSH2_SESSION *session, int blocking); +int ssh2_session_set_blocking(LIBSSH2_SESSION *session, int blocking); #endif /* LIBSSH2_SESSION_H */ diff --git a/src/sftp.c b/src/sftp.c index b3f37850..f22c234d 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -87,16 +87,16 @@ #define SSH_FXP_EXTENDED_REPLY 201 /* S_IFREG */ -#define LIBSSH2_SFTP_ATTR_PFILETYPE_FILE 0100000 +#define SSH2_SFTP_ATTR_PFILETYPE_FILE 0100000 /* S_IFDIR */ -#define LIBSSH2_SFTP_ATTR_PFILETYPE_DIR 0040000 +#define SSH2_SFTP_ATTR_PFILETYPE_DIR 0040000 #define SSH_FXE_STATVFS_ST_RDONLY 0x00000001 #define SSH_FXE_STATVFS_ST_NOSUID 0x00000002 /* This is the maximum packet length to accept, as larger than this indicate some kind of server problem. */ -#define LIBSSH2_SFTP_PACKET_MAXLEN (256 * 1024) +#define SFTP_PACKET_MAXLEN (256 * 1024) static int sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type, uint32_t request_id, unsigned char **data, @@ -112,13 +112,13 @@ static struct sftp_zombie_requests *find_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id) { struct sftp_zombie_requests *zombie = - _libssh2_list_first(&sftp->zombie_requests); + ssh2_list_first(&sftp->zombie_requests); while(zombie) { if(zombie->request_id == request_id) break; else - zombie = _libssh2_list_next(&zombie->node); + zombie = ssh2_list_next(&zombie->node); } return zombie; @@ -131,12 +131,12 @@ static void remove_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id) struct sftp_zombie_requests *zombie = find_zombie_request(sftp, request_id); if(zombie) { - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Removing request ID %u from the list of " - "zombie requests", request_id)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Removing request ID %u from the list of " + "zombie requests", request_id)); - _libssh2_list_remove(&zombie->node); - LIBSSH2_FREE(session, zombie); + ssh2_list_remove(&zombie->node); + SSH2_FREE(session, zombie); } } @@ -146,17 +146,17 @@ static int add_zombie_request(LIBSSH2_SFTP *sftp, uint32_t request_id) struct sftp_zombie_requests *zombie; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Marking request ID %u as a zombie request", request_id)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Marking request ID %u as a zombie request", request_id)); - zombie = LIBSSH2_ALLOC(sftp->channel->session, - sizeof(struct sftp_zombie_requests)); + zombie = SSH2_ALLOC(sftp->channel->session, + sizeof(struct sftp_zombie_requests)); if(!zombie) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "malloc fail for zombie request ID"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "malloc fail for zombie request ID"); else { zombie->request_id = request_id; - _libssh2_list_add(&sftp->zombie_requests, &zombie->node); + ssh2_list_add(&sftp->zombie_requests, &zombie->node); return LIBSSH2_ERROR_NONE; } } @@ -175,9 +175,8 @@ static int sftp_packet_add(LIBSSH2_SFTP *sftp, return LIBSSH2_ERROR_OUT_OF_BOUNDARY; } - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Received packet type %u (len %lu)", - (unsigned int)data[0], (unsigned long)data_len)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Received packet type %u (len %lu)", + (unsigned int)data[0], (unsigned long)data_len)); /* * Experience shows that if we mess up EAGAIN handling somewhere or @@ -217,14 +216,14 @@ static int sftp_packet_add(LIBSSH2_SFTP *sftp, break; default: sftp->last_errno = LIBSSH2_FX_OK; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Out of sync with the world"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Out of sync with the world"); } - request_id = _libssh2_ntohu32(&data[1]); + request_id = ssh2_ntohu32(&data[1]); - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Received packet id %d", - request_id)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Received packet id %d", + request_id)); /* Do not add the packet if it answers a request we have given up on. */ if((data[0] == SSH_FXP_STATUS || data[0] == SSH_FXP_DATA) && @@ -233,23 +232,23 @@ static int sftp_packet_add(LIBSSH2_SFTP *sftp, /* If we get here, the file ended before the response arrived. We are no longer interested in the request so we discard it */ - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); remove_zombie_request(sftp, request_id); return LIBSSH2_ERROR_NONE; } - packet = LIBSSH2_ALLOC(session, sizeof(struct sftp_packet)); + packet = SSH2_ALLOC(session, sizeof(struct sftp_packet)); if(!packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate datablock for SFTP packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate datablock for SFTP packet"); } packet->data = data; packet->data_len = data_len; packet->request_id = request_id; - _libssh2_list_add(&sftp->packets, &packet->node); + ssh2_list_add(&sftp->packets, &packet->node); return LIBSSH2_ERROR_NONE; } @@ -267,41 +266,41 @@ static int sftp_packet_read(LIBSSH2_SFTP *sftp) int packet_type; uint32_t request_id; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "recv packet")); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "recv packet")); switch(sftp->packet_state) { - case libssh2_NB_state_sent: /* EAGAIN from window adjusting */ - sftp->packet_state = libssh2_NB_state_idle; + case ssh2_NB_state_sent: /* EAGAIN from window adjusting */ + sftp->packet_state = ssh2_NB_state_idle; packet = sftp->partial_packet; goto window_adjust; - case libssh2_NB_state_sent1: /* EAGAIN from channel read */ - sftp->packet_state = libssh2_NB_state_idle; + case ssh2_NB_state_sent1: /* EAGAIN from channel read */ + sftp->packet_state = ssh2_NB_state_idle; packet = sftp->partial_packet; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "partial read cont, len: %u", sftp->partial_len)); - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "partial read cont, already recvd: %lu", - (unsigned long)sftp->partial_received)); - LIBSSH2_FALLTHROUGH(); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "partial read cont, len: %u", sftp->partial_len)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "partial read cont, already recvd: %lu", + (unsigned long)sftp->partial_received)); + SSH2_FALLTHROUGH(); default: if(!packet) { /* only do this if there is not already a packet buffer allocated to use */ /* each packet starts with a 32-bit length field */ - rc = _libssh2_channel_read(channel, 0, - (char *)&sftp->packet_header[ - sftp->packet_header_len], - sizeof(sftp->packet_header) - - sftp->packet_header_len); + rc = ssh2_channel_read(channel, 0, + (char *)&sftp->packet_header[ + sftp->packet_header_len], + sizeof(sftp->packet_header) - + sftp->packet_header_len); if(rc == LIBSSH2_ERROR_EAGAIN) return (int)rc; else if(rc < 0) - return _libssh2_error(session, (int)rc, "channel read"); + return ssh2_err(session, (int)rc, "channel read"); sftp->packet_header_len += rc; @@ -310,35 +309,34 @@ static int sftp_packet_read(LIBSSH2_SFTP *sftp) return LIBSSH2_ERROR_EAGAIN; /* parse SFTP packet header */ - sftp->partial_len = _libssh2_ntohu32(sftp->packet_header); + sftp->partial_len = ssh2_ntohu32(sftp->packet_header); packet_type = sftp->packet_header[4]; - request_id = _libssh2_ntohu32(sftp->packet_header + 5); + request_id = ssh2_ntohu32(sftp->packet_header + 5); /* make sure we do not proceed if the packet size is unreasonably large */ - if(sftp->partial_len > LIBSSH2_SFTP_PACKET_MAXLEN && + if(sftp->partial_len > SFTP_PACKET_MAXLEN && /* exception: response to SSH_FXP_READDIR request */ - !(sftp->readdir_state != libssh2_NB_state_idle && + !(sftp->readdir_state != ssh2_NB_state_idle && sftp->readdir_request_id == request_id && packet_type == SSH_FXP_NAME)) { libssh2_channel_flush(channel); sftp->packet_header_len = 0; - return _libssh2_error(session, - LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED, - "SFTP packet too large"); + return ssh2_err(session, LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED, + "SFTP packet too large"); } if(sftp->partial_len < 5) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Invalid SFTP packet size"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Invalid SFTP packet size"); - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Data begin - Packet Length: %lu", - (unsigned long)sftp->partial_len)); - packet = LIBSSH2_ALLOC(session, sftp->partial_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Data begin - Packet Length: %lu", + (unsigned long)sftp->partial_len)); + packet = SSH2_ALLOC(session, sftp->partial_len); if(!packet) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate SFTP packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate SFTP packet"); sftp->packet_header_len = 0; sftp->partial_packet = packet; /* copy over packet type(4) and request id(1) */ @@ -350,15 +348,14 @@ window_adjust: if(sftp->partial_len > recv_window) { /* ask for twice the data amount we need at once */ - rc = _libssh2_channel_receive_window_adjust(channel, - sftp->partial_len - * 2, - 1, NULL); + rc = ssh2_channel_receive_window_adjust(channel, + sftp->partial_len * 2, + 1, NULL); /* store the state so that we continue with the correct operation at next invoke */ sftp->packet_state = (rc == LIBSSH2_ERROR_EAGAIN) ? - libssh2_NB_state_sent : - libssh2_NB_state_idle; + ssh2_NB_state_sent : + ssh2_NB_state_idle; if(rc == LIBSSH2_ERROR_EAGAIN) return (int)rc; @@ -367,10 +364,9 @@ window_adjust: /* Read as much of the packet as we can */ while(sftp->partial_len > sftp->partial_received) { - rc = _libssh2_channel_read(channel, 0, - (char *)&packet[sftp->partial_received], - sftp->partial_len - - sftp->partial_received); + rc = ssh2_channel_read(channel, 0, + (char *)&packet[sftp->partial_received], + sftp->partial_len - sftp->partial_received); if(rc == LIBSSH2_ERROR_EAGAIN) { /* @@ -378,14 +374,14 @@ window_adjust: * the caller. Set 'partial_packet' so that this function * knows how to continue on the next invoke. */ - sftp->packet_state = libssh2_NB_state_sent1; + sftp->packet_state = ssh2_NB_state_sent1; return (int)rc; } else if(rc < 0) { - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); sftp->partial_packet = NULL; - return _libssh2_error(session, (int)rc, - "Error waiting for SFTP packet"); + return ssh2_err(session, (int)rc, + "Error waiting for SFTP packet"); } sftp->partial_received += rc; } @@ -397,7 +393,7 @@ window_adjust: packet_type = packet[0]; rc = sftp_packet_add(sftp, packet, sftp->partial_len); if(rc) { - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); return (int)rc; } else { @@ -418,12 +414,12 @@ static void sftp_packetlist_flush(LIBSSH2_SFTP_HANDLE *handle) LIBSSH2_SESSION *session = sftp->channel->session; /* remove pending packets, if any */ - chunk = _libssh2_list_first(&handle->packet_list); + chunk = ssh2_list_first(&handle->packet_list); while(chunk) { unsigned char *data; size_t data_len; int rc; - struct sftp_pipeline_chunk *next = _libssh2_list_next(&chunk->node); + struct sftp_pipeline_chunk *next = ssh2_list_next(&chunk->node); rc = sftp_packet_ask(sftp, SSH_FXP_STATUS, chunk->request_id, &data, &data_len); @@ -433,14 +429,14 @@ static void sftp_packetlist_flush(LIBSSH2_SFTP_HANDLE *handle) if(!rc) /* we found a packet, free it */ - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); else if(chunk->sent) /* there was no incoming packet for this request, mark this request as a zombie if it ever sent the request */ add_zombie_request(sftp, chunk->request_id); - _libssh2_list_remove(&chunk->node); - LIBSSH2_FREE(session, chunk); + ssh2_list_remove(&chunk->node); + SSH2_FREE(session, chunk); chunk = next; } } @@ -453,7 +449,7 @@ static int sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type, unsigned char **data, size_t *data_len) { LIBSSH2_SESSION *session = sftp->channel->session; - struct sftp_packet *packet = _libssh2_list_first(&sftp->packets); + struct sftp_packet *packet = ssh2_list_first(&sftp->packets); if(!packet) return -1; @@ -470,19 +466,19 @@ static int sftp_packet_ask(LIBSSH2_SFTP *sftp, unsigned char packet_type, *data_len = packet->data_len; /* unlink and free this struct */ - _libssh2_list_remove(&packet->node); - LIBSSH2_FREE(session, packet); + ssh2_list_remove(&packet->node); + SSH2_FREE(session, packet); return 0; } /* check next struct in the list */ - packet = _libssh2_list_next(&packet->node); + packet = ssh2_list_next(&packet->node); } return -1; } /* - * A la libssh2_packet_require + * A la ssh2_packet_require() */ static int sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type, uint32_t request_id, @@ -496,13 +492,13 @@ static int sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type, return LIBSSH2_ERROR_BAD_USE; } - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Requiring packet %u id %u", - (unsigned int)packet_type, request_id)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Requiring packet %u id %u", + (unsigned int)packet_type, request_id)); if(sftp_packet_ask(sftp, packet_type, request_id, data, data_len) == 0) { /* The right packet was available in the packet brigade */ - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Got %u", - (unsigned int)packet_type)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Got %u", + (unsigned int)packet_type)); if(*data_len < required_size) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; @@ -511,7 +507,7 @@ static int sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type, return LIBSSH2_ERROR_NONE; } - while(session->socket_state == LIBSSH2_SOCKET_CONNECTED) { + while(session->socket_state == SSH2_SOCKET_CONNECTED) { rc = sftp_packet_read(sftp); if(rc < 0) return rc; @@ -519,8 +515,8 @@ static int sftp_packet_require(LIBSSH2_SFTP *sftp, unsigned char packet_type, /* data was read, check the queue again */ if(!sftp_packet_ask(sftp, packet_type, request_id, data, data_len)) { /* The right packet was available in the packet brigade */ - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Got %d", - (unsigned int)packet_type)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Got %d", + (unsigned int)packet_type)); if(*data_len < required_size) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; @@ -554,7 +550,7 @@ static int sftp_packet_requirev(LIBSSH2_SFTP *sftp, int num_valid_responses, if(sftp->requirev_start == 0) sftp->requirev_start = time(NULL); - while(sftp->channel->session->socket_state == LIBSSH2_SOCKET_CONNECTED) { + while(sftp->channel->session->socket_state == SSH2_SOCKET_CONNECTED) { for(i = 0; i < num_valid_responses; i++) { if(sftp_packet_ask(sftp, valid_responses[i], request_id, data, data_len) == 0) { @@ -629,28 +625,28 @@ static ssize_t sftp_attr2bin(unsigned char *p, bits */ if(!attrs) { - _libssh2_htonu32(s, 0); + ssh2_htonu32(s, 0); return 4; } - _libssh2_store_u32(&s, (uint32_t)(attrs->flags & flag_mask)); + ssh2_store_u32(&s, (uint32_t)(attrs->flags & flag_mask)); if(attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) { - _libssh2_store_u64(&s, attrs->filesize); + ssh2_store_u64(&s, attrs->filesize); } if(attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) { - _libssh2_store_u32(&s, (uint32_t)attrs->uid); - _libssh2_store_u32(&s, (uint32_t)attrs->gid); + ssh2_store_u32(&s, (uint32_t)attrs->uid); + ssh2_store_u32(&s, (uint32_t)attrs->gid); } if(attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { - _libssh2_store_u32(&s, (uint32_t)attrs->permissions); + ssh2_store_u32(&s, (uint32_t)attrs->permissions); } if(attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) { - _libssh2_store_u32(&s, (uint32_t)attrs->atime); - _libssh2_store_u32(&s, (uint32_t)attrs->mtime); + ssh2_store_u32(&s, (uint32_t)attrs->atime); + ssh2_store_u32(&s, (uint32_t)attrs->mtime); } return s - p; @@ -661,17 +657,17 @@ static ssize_t sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, { struct string_buf buf; uint32_t flags = 0; - buf.data = (unsigned char *)LIBSSH2_UNCONST(p); + buf.data = (unsigned char *)SSH2_UNCONST(p); buf.dataptr = buf.data; buf.len = data_len; - if(_libssh2_get_u32(&buf, &flags)) { + if(ssh2_get_u32(&buf, &flags)) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } attrs->flags = flags; if(attrs->flags & LIBSSH2_SFTP_ATTR_SIZE) { - if(_libssh2_get_u64(&buf, &(attrs->filesize))) { + if(ssh2_get_u64(&buf, &(attrs->filesize))) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } } @@ -679,8 +675,8 @@ static ssize_t sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, if(attrs->flags & LIBSSH2_SFTP_ATTR_UIDGID) { uint32_t uid = 0; uint32_t gid = 0; - if(_libssh2_get_u32(&buf, &uid) || - _libssh2_get_u32(&buf, &gid)) { + if(ssh2_get_u32(&buf, &uid) || + ssh2_get_u32(&buf, &gid)) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } attrs->uid = uid; @@ -689,7 +685,7 @@ static ssize_t sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, if(attrs->flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) { uint32_t permissions; - if(_libssh2_get_u32(&buf, &permissions)) { + if(ssh2_get_u32(&buf, &permissions)) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } attrs->permissions = permissions; @@ -698,8 +694,8 @@ static ssize_t sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, if(attrs->flags & LIBSSH2_SFTP_ATTR_ACMODTIME) { uint32_t atime; uint32_t mtime; - if(_libssh2_get_u32(&buf, &atime) || - _libssh2_get_u32(&buf, &mtime)) { + if(ssh2_get_u32(&buf, &atime) || + ssh2_get_u32(&buf, &mtime)) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } attrs->atime = atime; @@ -715,13 +711,13 @@ static ssize_t sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, size_t edata_len; unsigned char *edata; - if(_libssh2_get_u32(&buf, &extended_count)) { + if(ssh2_get_u32(&buf, &extended_count)) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } for(i = 0; i < extended_count; ++i) { - if(_libssh2_get_string(&buf, &etype, &etype_len) || - _libssh2_get_string(&buf, &edata, &edata_len)) { + if(ssh2_get_string(&buf, &etype, &etype_len) || + ssh2_get_string(&buf, &edata, &edata_len)) { return LIBSSH2_ERROR_BUFFER_TOO_SMALL; } } @@ -734,12 +730,12 @@ static ssize_t sftp_bin2attr(LIBSSH2_SFTP_ATTRIBUTES *attrs, * SFTP API * ********** */ -LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor); +LIBSSH2_CHANNEL_CLOSE_FUNC(sftp_dtor); /* * Shutdown an SFTP stream when the channel closes */ -LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor) +LIBSSH2_CHANNEL_CLOSE_FUNC(sftp_dtor) { LIBSSH2_SFTP *sftp = (LIBSSH2_SFTP *)(*channel_abstract); @@ -748,15 +744,15 @@ LIBSSH2_CHANNEL_CLOSE_FUNC(libssh2_sftp_dtor) /* Free the partial packet storage for sftp_packet_read */ if(sftp->partial_packet) { - LIBSSH2_FREE(session, sftp->partial_packet); + SSH2_FREE(session, sftp->partial_packet); } - /* Free the packet storage for _libssh2_sftp_packet_readdir */ + /* Free the packet storage for ssh2_sftp_packet_readdir() */ if(sftp->readdir_packet) { - LIBSSH2_FREE(session, sftp->readdir_packet); + SSH2_FREE(session, sftp->readdir_packet); } - LIBSSH2_FREE(session, sftp); + SSH2_FREE(session, sftp); } /* @@ -771,9 +767,8 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) struct string_buf buf; unsigned char *endp; - if(session->sftpInit_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Initializing SFTP subsystem")); + if(session->sftpInit_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Initializing SFTP subsystem")); /* * The 'sftpInit_sftp' and 'sftpInit_channel' struct fields within the @@ -789,98 +784,96 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) assert(!session->sftpInit_sftp); session->sftpInit_sftp = NULL; - session->sftpInit_state = libssh2_NB_state_created; + session->sftpInit_state = ssh2_NB_state_created; } sftp_handle = session->sftpInit_sftp; - if(session->sftpInit_state == libssh2_NB_state_created) { + if(session->sftpInit_state == ssh2_NB_state_created) { session->sftpInit_channel = - _libssh2_channel_open(session, "session", sizeof("session") - 1, - LIBSSH2_CHANNEL_WINDOW_DEFAULT, - LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0); + ssh2_channel_open(session, "session", sizeof("session") - 1, + LIBSSH2_CHANNEL_WINDOW_DEFAULT, + LIBSSH2_CHANNEL_PACKET_DEFAULT, NULL, 0); if(!session->sftpInit_channel) { if(libssh2_session_last_errno(session) == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block starting up channel"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block starting up channel"); } else { - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Unable to startup channel"); - session->sftpInit_state = libssh2_NB_state_idle; + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Unable to startup channel"); + session->sftpInit_state = ssh2_NB_state_idle; } return NULL; } - session->sftpInit_state = libssh2_NB_state_sent; + session->sftpInit_state = ssh2_NB_state_sent; } - if(session->sftpInit_state == libssh2_NB_state_sent) { - int ret = _libssh2_channel_process_startup(session->sftpInit_channel, - "subsystem", - sizeof("subsystem") - 1, - "sftp", strlen("sftp")); + if(session->sftpInit_state == ssh2_NB_state_sent) { + int ret = ssh2_channel_process_startup(session->sftpInit_channel, + "subsystem", + sizeof("subsystem") - 1, + "sftp", strlen("sftp")); if(ret == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block to request SFTP subsystem"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block to request SFTP subsystem"); return NULL; } else if(ret) { - _libssh2_error(session, LIBSSH2_ERROR_CHANNEL_FAILURE, - "Unable to request SFTP subsystem"); + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Unable to request SFTP subsystem"); goto sftp_init_error; } - session->sftpInit_state = libssh2_NB_state_sent1; + session->sftpInit_state = ssh2_NB_state_sent1; } - if(session->sftpInit_state == libssh2_NB_state_sent1) { - rc = _libssh2_channel_extended_data(session->sftpInit_channel, - LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); + if(session->sftpInit_state == ssh2_NB_state_sent1) { + rc = ssh2_channel_extended_data(session->sftpInit_channel, + LIBSSH2_CHANNEL_EXTENDED_DATA_IGNORE); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block requesting handle extended data"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block requesting handle extended data"); return NULL; } - sftp_handle = - session->sftpInit_sftp = - LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP)); + sftp_handle = session->sftpInit_sftp = + SSH2_CALLOC(session, sizeof(LIBSSH2_SFTP)); if(!sftp_handle) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate a new SFTP structure"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate a new SFTP structure"); goto sftp_init_error; } sftp_handle->channel = session->sftpInit_channel; sftp_handle->request_id = 0; - _libssh2_htonu32(session->sftpInit_buffer, 5); + ssh2_htonu32(session->sftpInit_buffer, 5); session->sftpInit_buffer[4] = SSH_FXP_INIT; - _libssh2_htonu32(session->sftpInit_buffer + 5, LIBSSH2_SFTP_VERSION); + ssh2_htonu32(session->sftpInit_buffer + 5, LIBSSH2_SFTP_VERSION); session->sftpInit_sent = 0; /* nothing's sent yet */ - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Sending FXP_INIT packet advertising " - "version %d support", - (int)LIBSSH2_SFTP_VERSION)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Sending FXP_INIT packet advertising " + "version %d support", (int)LIBSSH2_SFTP_VERSION)); - session->sftpInit_state = libssh2_NB_state_sent2; + session->sftpInit_state = ssh2_NB_state_sent2; } - if(session->sftpInit_state == libssh2_NB_state_sent2) { + if(session->sftpInit_state == ssh2_NB_state_sent2) { /* sent off what's left of the init buffer to send */ - rc = _libssh2_channel_write(session->sftpInit_channel, 0, - session->sftpInit_buffer + - session->sftpInit_sent, - 9 - session->sftpInit_sent); + rc = ssh2_channel_write(session->sftpInit_channel, 0, + session->sftpInit_buffer + + session->sftpInit_sent, + 9 - session->sftpInit_sent); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block sending SSH_FXP_INIT"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block sending SSH_FXP_INIT"); return NULL; } else if(rc < 0) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send SSH_FXP_INIT"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send SSH_FXP_INIT"); goto sftp_init_error; } else { @@ -889,46 +882,46 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) if(session->sftpInit_sent == 9) /* move on */ - session->sftpInit_state = libssh2_NB_state_sent3; + session->sftpInit_state = ssh2_NB_state_sent3; /* if less than 9, we remain in this state to send more later on */ } } - if(session->sftpInit_state == libssh2_NB_state_error_closing) { - rc = _libssh2_channel_free(session->sftpInit_channel); + if(session->sftpInit_state == ssh2_NB_state_error_closing) { + rc = ssh2_channel_free(session->sftpInit_channel); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block closing channel"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block closing channel"); return NULL; } session->sftpInit_channel = NULL; if(session->sftpInit_sftp) { - LIBSSH2_FREE(session, session->sftpInit_sftp); + SSH2_FREE(session, session->sftpInit_sftp); session->sftpInit_sftp = NULL; } - session->sftpInit_state = libssh2_NB_state_idle; + session->sftpInit_state = ssh2_NB_state_idle; return NULL; } rc = sftp_packet_require(sftp_handle, SSH_FXP_VERSION, 0, &data, &data_len, 5); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block receiving SSH_FXP_VERSION"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block receiving SSH_FXP_VERSION"); return NULL; } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Invalid SSH_FXP_VERSION response"); + ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Invalid SSH_FXP_VERSION response"); goto sftp_init_error; } else if(rc) { - _libssh2_error(session, (int)rc, - "Timeout waiting for response from SFTP subsystem"); + ssh2_err(session, (int)rc, + "Timeout waiting for response from SFTP subsystem"); goto sftp_init_error; } @@ -937,79 +930,78 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) buf.len = data_len; endp = &buf.data[data_len]; - if(_libssh2_get_u32(&buf, &(sftp_handle->version))) { - LIBSSH2_FREE(session, data); - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short when extracting version"); + if(ssh2_get_u32(&buf, &(sftp_handle->version))) { + SSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short when extracting version"); goto sftp_init_error; } if(sftp_handle->version > LIBSSH2_SFTP_VERSION) { - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Truncating remote SFTP version from %u", - sftp_handle->version)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Truncating remote SFTP version from %u", + sftp_handle->version)); sftp_handle->version = LIBSSH2_SFTP_VERSION; } - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Enabling SFTP version %u compatibility", - sftp_handle->version)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Enabling SFTP version %u compatibility", sftp_handle->version)); while(buf.dataptr < endp) { unsigned char *extname, *extdata; size_t extname_len, extdata_len; uint32_t extversion = 0; - if(_libssh2_get_string(&buf, &extname, &extname_len)) { - LIBSSH2_FREE(session, data); - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short when extracting extname"); + if(ssh2_get_string(&buf, &extname, &extname_len)) { + SSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short when extracting extname"); goto sftp_init_error; } - if(_libssh2_get_string(&buf, &extdata, &extdata_len)) { - LIBSSH2_FREE(session, data); - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Data too short when extracting extdata"); + if(ssh2_get_string(&buf, &extdata, &extdata_len)) { + SSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Data too short when extracting extdata"); goto sftp_init_error; } if(extdata_len > 0) { char *extversion_str; - extversion_str = (char *)LIBSSH2_ALLOC(session, extdata_len + 1); + extversion_str = (char *)SSH2_ALLOC(session, extdata_len + 1); if(!extversion_str) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for SSH_FXP_VERSION " - "packet"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for SSH_FXP_VERSION " + "packet"); goto sftp_init_error; } memcpy(extversion_str, extdata, extdata_len); extversion_str[extdata_len] = '\0'; extversion = (uint32_t)strtol(extversion_str, NULL, 10); - LIBSSH2_FREE(session, extversion_str); + SSH2_FREE(session, extversion_str); } if(extname_len == 24 && strncmp("posix-rename@openssh.com", (char *)extname, 24) == 0) { sftp_handle->posix_rename_version = extversion; } } - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); /* Make sure that when the channel gets closed, the SFTP service is shut down too */ sftp_handle->channel->abstract = sftp_handle; - sftp_handle->channel->close_cb = libssh2_sftp_dtor; + sftp_handle->channel->close_cb = sftp_dtor; - session->sftpInit_state = libssh2_NB_state_idle; + session->sftpInit_state = ssh2_NB_state_idle; /* clear the sftp and channel pointers in this session struct now */ session->sftpInit_sftp = NULL; session->sftpInit_channel = NULL; - _libssh2_list_init(&sftp_handle->sftp_handles); + ssh2_list_init(&sftp_handle->sftp_handles); return sftp_handle; sftp_init_error: - session->sftpInit_state = libssh2_NB_state_error_closing; + session->sftpInit_state = ssh2_NB_state_error_closing; return NULL; } @@ -1024,9 +1016,9 @@ LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session) if(!session) return NULL; - if(!(session->state & LIBSSH2_STATE_AUTHENTICATED)) { - _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "session not authenticated yet"); + if(!(session->state & SSH2_STATE_AUTHENTICATED)) { + ssh2_err(session, LIBSSH2_ERROR_INVAL, + "session not authenticated yet"); return NULL; } @@ -1045,55 +1037,55 @@ static int sftp_shutdown(LIBSSH2_SFTP *sftp) * Make sure all memory used in the state variables are free */ if(sftp->partial_packet) { - LIBSSH2_FREE(session, sftp->partial_packet); + SSH2_FREE(session, sftp->partial_packet); sftp->partial_packet = NULL; } if(sftp->open_packet) { - LIBSSH2_FREE(session, sftp->open_packet); + SSH2_FREE(session, sftp->open_packet); sftp->open_packet = NULL; } if(sftp->readdir_packet) { - LIBSSH2_FREE(session, sftp->readdir_packet); + SSH2_FREE(session, sftp->readdir_packet); sftp->readdir_packet = NULL; } if(sftp->fstat_packet) { - LIBSSH2_FREE(session, sftp->fstat_packet); + SSH2_FREE(session, sftp->fstat_packet); sftp->fstat_packet = NULL; } if(sftp->unlink_packet) { - LIBSSH2_FREE(session, sftp->unlink_packet); + SSH2_FREE(session, sftp->unlink_packet); sftp->unlink_packet = NULL; } if(sftp->rename_packet) { - LIBSSH2_FREE(session, sftp->rename_packet); + SSH2_FREE(session, sftp->rename_packet); sftp->rename_packet = NULL; } if(sftp->fstatvfs_packet) { - LIBSSH2_FREE(session, sftp->fstatvfs_packet); + SSH2_FREE(session, sftp->fstatvfs_packet); sftp->fstatvfs_packet = NULL; } if(sftp->statvfs_packet) { - LIBSSH2_FREE(session, sftp->statvfs_packet); + SSH2_FREE(session, sftp->statvfs_packet); sftp->statvfs_packet = NULL; } if(sftp->mkdir_packet) { - LIBSSH2_FREE(session, sftp->mkdir_packet); + SSH2_FREE(session, sftp->mkdir_packet); sftp->mkdir_packet = NULL; } if(sftp->rmdir_packet) { - LIBSSH2_FREE(session, sftp->rmdir_packet); + SSH2_FREE(session, sftp->rmdir_packet); sftp->rmdir_packet = NULL; } if(sftp->stat_packet) { - LIBSSH2_FREE(session, sftp->stat_packet); + SSH2_FREE(session, sftp->stat_packet); sftp->stat_packet = NULL; } if(sftp->symlink_packet) { - LIBSSH2_FREE(session, sftp->symlink_packet); + SSH2_FREE(session, sftp->symlink_packet); sftp->symlink_packet = NULL; } if(sftp->fsync_packet) { - LIBSSH2_FREE(session, sftp->fsync_packet); + SSH2_FREE(session, sftp->fsync_packet); sftp->fsync_packet = NULL; } @@ -1102,7 +1094,7 @@ static int sftp_shutdown(LIBSSH2_SFTP *sftp) /* TODO: We should consider walking over the sftp_handles list and kill * any remaining sftp handles ... */ - rc = _libssh2_channel_free(sftp->channel); + rc = ssh2_channel_free(sftp->channel); return rc; } @@ -1144,19 +1136,17 @@ static LIBSSH2_SFTP_HANDLE *sftp_open(LIBSSH2_SFTP *sftp, /* packet_len(4) + packet_type(1) + request_id(4) + filename_len(4) + flags(4) */ - packet_len = (13 + - (open_file ? (4 + sftp_attrsize(attrs.flags)) : 0)); + packet_len = (13 + (open_file ? (4 + sftp_attrsize(attrs.flags)) : 0)); if(packet_len + (uint32_t)filename_len < packet_len) { - _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_open"); + ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_open"); return NULL; } packet_len += (uint32_t)filename_len; - if(sftp->open_state == libssh2_NB_state_idle) { + if(sftp->open_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; @@ -1168,51 +1158,50 @@ static LIBSSH2_SFTP_HANDLE *sftp_open(LIBSSH2_SFTP *sftp, /* surprise! this starts out with nothing sent */ sftp->open_packet_sent = 0; - s = sftp->open_packet = LIBSSH2_ALLOC(session, sftp->open_packet_len); + s = sftp->open_packet = SSH2_ALLOC(session, sftp->open_packet_len); if(!sftp->open_packet) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_OPEN or " - "FXP_OPENDIR packet"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_OPEN or " + "FXP_OPENDIR packet"); return NULL; } /* Filetype in SFTP 3 and earlier */ attrs.permissions = mode | - (open_file ? LIBSSH2_SFTP_ATTR_PFILETYPE_FILE : - LIBSSH2_SFTP_ATTR_PFILETYPE_DIR); + (open_file ? SSH2_SFTP_ATTR_PFILETYPE_FILE : + SSH2_SFTP_ATTR_PFILETYPE_DIR); - _libssh2_store_u32(&s, sftp->open_packet_len - 4); + ssh2_store_u32(&s, sftp->open_packet_len - 4); *(s++) = open_file ? SSH_FXP_OPEN : SSH_FXP_OPENDIR; sftp->open_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->open_request_id); - _libssh2_store_str(&s, filename, filename_len); + ssh2_store_u32(&s, sftp->open_request_id); + ssh2_store_str(&s, filename, filename_len); if(open_file) { - _libssh2_store_u32(&s, flags); + ssh2_store_u32(&s, flags); s += sftp_attr2bin(s, &attrs); } - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Sending %s open request", - open_file ? "file" : "directory")); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Sending %s open request", + open_file ? "file" : "directory")); - sftp->open_state = libssh2_NB_state_created; + sftp->open_state = ssh2_NB_state_created; } - if(sftp->open_state == libssh2_NB_state_created) { - rc = _libssh2_channel_write(channel, 0, sftp->open_packet + - sftp->open_packet_sent, - sftp->open_packet_len - - sftp->open_packet_sent); + if(sftp->open_state == ssh2_NB_state_created) { + rc = ssh2_channel_write(channel, 0, sftp->open_packet + + sftp->open_packet_sent, + sftp->open_packet_len - + sftp->open_packet_sent); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block sending FXP_OPEN or " - "FXP_OPENDIR command"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block sending FXP_OPEN or FXP_OPENDIR command"); return NULL; } else if(rc < 0) { - _libssh2_error(session, (int)rc, "Unable to send FXP_OPEN*"); - LIBSSH2_FREE(session, sftp->open_packet); + ssh2_err(session, (int)rc, "Unable to send FXP_OPEN*"); + SSH2_FREE(session, sftp->open_packet); sftp->open_packet = NULL; - sftp->open_state = libssh2_NB_state_idle; + sftp->open_state = ssh2_NB_state_idle; return NULL; } @@ -1221,38 +1210,38 @@ static LIBSSH2_SFTP_HANDLE *sftp_open(LIBSSH2_SFTP *sftp, sftp->open_packet_sent += rc; if(sftp->open_packet_len == sftp->open_packet_sent) { - LIBSSH2_FREE(session, sftp->open_packet); + SSH2_FREE(session, sftp->open_packet); sftp->open_packet = NULL; - sftp->open_state = libssh2_NB_state_sent; + sftp->open_state = ssh2_NB_state_sent; } } - if(sftp->open_state == libssh2_NB_state_sent) { + if(sftp->open_state == ssh2_NB_state_sent) { size_t data_len = 0; unsigned char *data; - static const unsigned char fopen_responses[2] = - { SSH_FXP_HANDLE, SSH_FXP_STATUS }; + static const unsigned char fopen_responses[2] = { + SSH_FXP_HANDLE, SSH_FXP_STATUS + }; rc = sftp_packet_requirev(sftp, 2, fopen_responses, sftp->open_request_id, &data, &data_len, 1); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for status message"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for status message"); return NULL; } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Response too small"); + ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Response too small"); return NULL; } - sftp->open_state = libssh2_NB_state_idle; + sftp->open_state = ssh2_NB_state_idle; if(rc) { - _libssh2_error(session, (int)rc, - "Timeout waiting for status message"); + ssh2_err(session, (int)rc, "Timeout waiting for status message"); return NULL; } @@ -1264,19 +1253,18 @@ static LIBSSH2_SFTP_HANDLE *sftp_open(LIBSSH2_SFTP *sftp, int badness = 1; if(data_len < 9) { - _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Too small FXP_STATUS"); - LIBSSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Too small FXP_STATUS"); + SSH2_FREE(session, data); return NULL; } - sftp->last_errno = _libssh2_ntohu32(data + 5); + sftp->last_errno = ssh2_ntohu32(data + 5); if(LIBSSH2_FX_OK == sftp->last_errno) { - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "got HANDLE FXOK")); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "got HANDLE FXOK")); - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); /* silly situation, but check for a HANDLE */ rc = sftp_packet_require(sftp, SSH_FXP_HANDLE, @@ -1284,15 +1272,15 @@ static LIBSSH2_SFTP_HANDLE *sftp_open(LIBSSH2_SFTP *sftp, &data_len, 10); if(rc == LIBSSH2_ERROR_EAGAIN) { /* go back to sent state and wait for something else */ - sftp->open_state = libssh2_NB_state_sent; + sftp->open_state = ssh2_NB_state_sent; return NULL; } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Too small FXP_HANDLE"); + ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Too small FXP_HANDLE"); return NULL; } else if(!rc) @@ -1301,34 +1289,33 @@ static LIBSSH2_SFTP_HANDLE *sftp_open(LIBSSH2_SFTP *sftp, } if(badness) { - _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Failed opening remote file"); - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "got FXP_STATUS %d", - sftp->last_errno)); - LIBSSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Failed opening remote file"); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "got FXP_STATUS %d", + sftp->last_errno)); + SSH2_FREE(session, data); return NULL; } } if(data_len < 10) { - _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Too small FXP_HANDLE"); - LIBSSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Too small FXP_HANDLE"); + SSH2_FREE(session, data); return NULL; } - fp = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE)); + fp = SSH2_CALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE)); if(!fp) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate new SFTP handle structure"); - LIBSSH2_FREE(session, data); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate new SFTP handle structure"); + SSH2_FREE(session, data); return NULL; } fp->handle_type = open_file ? LIBSSH2_SFTP_HANDLE_FILE : LIBSSH2_SFTP_HANDLE_DIR; - fp->handle_len = _libssh2_ntohu32(data + 5); + fp->handle_len = ssh2_ntohu32(data + 5); if(fp->handle_len > SFTP_HANDLE_MAXLEN) /* SFTP does not allow handles longer than 256 characters */ fp->handle_len = SFTP_HANDLE_MAXLEN; @@ -1339,18 +1326,17 @@ static LIBSSH2_SFTP_HANDLE *sftp_open(LIBSSH2_SFTP *sftp, memcpy(fp->handle, data + 9, fp->handle_len); - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); /* add this file handle to the list kept in the sftp session */ - _libssh2_list_add(&sftp->sftp_handles, &fp->node); + ssh2_list_add(&sftp->sftp_handles, &fp->node); fp->sftp = sftp; /* point to the parent struct */ fp->u.file.offset = 0; fp->u.file.offset_sent = 0; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Open command successful")); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Open command successful")); return fp; } return NULL; @@ -1406,7 +1392,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, struct sftp_pipeline_chunk *chunk; struct sftp_pipeline_chunk *next; ssize_t rc; - struct libssh2_sftp_handle_file_data *filep = &handle->u.file; + struct ssh2_sftp_handle_file_data *filep = &handle->u.file; size_t bytes_in_buffer = 0; char *sliding_bufferp = buffer; @@ -1422,27 +1408,27 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, what phase of execution we were at. The state is stored in sftp->read_state. - libssh2_NB_state_idle: The first phase is where we prepare multiple + ssh2_NB_state_idle: The first phase is where we prepare multiple FXP_READ packets to do optimistic read-ahead. We send off as many as possible in the second phase without waiting for a response to each one; this is the key to fast reads. We may have to adjust the channel window size to do this which may interrupt this function while waiting. - The state machine saves the phase as libssh2_NB_state_idle so it + The state machine saves the phase as ssh2_NB_state_idle so it returns here on the next call. - libssh2_NB_state_sent: The second phase is where we send the FXP_READ + ssh2_NB_state_sent: The second phase is where we send the FXP_READ packets. Writing them to the channel can be interrupted with EAGAIN but the state machine ensures we skip the first phase on the next call and resume sending. - libssh2_NB_state_sent2: In the third phase (indicated by ) we read the + ssh2_NB_state_sent2: In the third phase (indicated by ) we read the data from the responses that have arrived so far. Reading can be interrupted with EAGAIN but the state machine ensures we skip the first and second phases on the next call and resume sending. */ switch(sftp->read_state) { - case libssh2_NB_state_idle: + case ssh2_NB_state_idle: sftp->last_errno = LIBSSH2_FX_OK; /* Some data may already have been read from the server in the @@ -1450,7 +1436,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, return that now as we cannot risk being interrupted later with data partially written to the buffer. */ if(filep->data_left) { - size_t copy = LIBSSH2_MIN(buffer_size, filep->data_left); + size_t copy = SSH2_MIN(buffer_size, filep->data_left); memcpy(buffer, &filep->data[filep->data_len - filep->data_left], copy); @@ -1459,7 +1445,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, filep->offset += copy; if(!filep->data_left) { - LIBSSH2_FREE(session, filep->data); + SSH2_FREE(session, filep->data); filep->data = NULL; } @@ -1513,9 +1499,8 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, /* more data is asked for than what the window currently allows, expand it! */ - rc = _libssh2_channel_receive_window_adjust(sftp->channel, - (uint32_t)(max_read_ahead * 8), - 1, NULL); + rc = ssh2_channel_receive_window_adjust( + sftp->channel, (uint32_t)(max_read_ahead * 8), 1, NULL); /* if this returns EAGAIN, we get back to this function at next call */ assert(rc != LIBSSH2_ERROR_EAGAIN || !filep->data_left); @@ -1539,11 +1524,11 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, if(size > MAX_SFTP_READ_SIZE) size = MAX_SFTP_READ_SIZE; - chunk = LIBSSH2_ALLOC(session, packet_len + - sizeof(struct sftp_pipeline_chunk)); + chunk = SSH2_ALLOC(session, packet_len + + sizeof(struct sftp_pipeline_chunk)); if(!chunk) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "malloc fail for FXP_WRITE"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "malloc fail for FXP_WRITE"); chunk->offset = filep->offset_sent; chunk->len = size; @@ -1552,43 +1537,43 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, s = chunk->packet; - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *s++ = SSH_FXP_READ; request_id = sftp->request_id++; chunk->request_id = request_id; - _libssh2_store_u32(&s, request_id); - _libssh2_store_str(&s, handle->handle, handle->handle_len); - _libssh2_store_u64(&s, filep->offset_sent); + ssh2_store_u32(&s, request_id); + ssh2_store_str(&s, handle->handle, handle->handle_len); + ssh2_store_u64(&s, filep->offset_sent); filep->offset_sent += size; /* advance offset at once */ - _libssh2_store_u32(&s, size); + ssh2_store_u32(&s, size); /* add this new entry LAST in the list */ - _libssh2_list_add(&handle->packet_list, &chunk->node); + ssh2_list_add(&handle->packet_list, &chunk->node); /* deduct the size we used, as we might have to create more packets */ - count -= LIBSSH2_MIN(size, count); - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "read request id %d sent (offset: %lu, size: %lu)", - request_id, (unsigned long)chunk->offset, - (unsigned long)chunk->len)); + count -= SSH2_MIN(size, count); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "read request id %d sent (offset: %lu, size: %lu)", + request_id, (unsigned long)chunk->offset, + (unsigned long)chunk->len)); } - LIBSSH2_FALLTHROUGH(); - case libssh2_NB_state_sent: + SSH2_FALLTHROUGH(); + case ssh2_NB_state_sent: - sftp->read_state = libssh2_NB_state_idle; + sftp->read_state = ssh2_NB_state_idle; /* move through the READ packets that have not been sent and send as many as possible - remember that we do not block */ - chunk = _libssh2_list_first(&handle->packet_list); + chunk = ssh2_list_first(&handle->packet_list); while(chunk) { if(chunk->lefttosend) { - rc = _libssh2_channel_write(channel, 0, - &chunk->packet[chunk->sent], - chunk->lefttosend); + rc = ssh2_channel_write(channel, 0, + &chunk->packet[chunk->sent], + chunk->lefttosend); if(rc < 0) { - sftp->read_state = libssh2_NB_state_sent; + sftp->read_state = ssh2_NB_state_sent; return rc; } @@ -1600,7 +1585,7 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, /* We still have data left to send for this chunk. * If there is at least one completely sent chunk, * we can get out of this loop and start reading. */ - if(chunk != _libssh2_list_first(&handle->packet_list)) { + if(chunk != ssh2_list_first(&handle->packet_list)) { break; } else { @@ -1610,18 +1595,18 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, } /* move on to the next chunk with data to send */ - chunk = _libssh2_list_next(&chunk->node); + chunk = ssh2_list_next(&chunk->node); } - LIBSSH2_FALLTHROUGH(); + SSH2_FALLTHROUGH(); - case libssh2_NB_state_sent2: + case ssh2_NB_state_sent2: - sftp->read_state = libssh2_NB_state_idle; + sftp->read_state = ssh2_NB_state_idle; /* * Count all ACKed packets and act on the contents of them. */ - chunk = _libssh2_list_first(&handle->packet_list); + chunk = ssh2_list_first(&handle->packet_list); while(chunk) { unsigned char *data; @@ -1639,8 +1624,8 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, } else { /* we should never reach this point */ - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "sftp_read() internal error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "sftp_read() internal error"); } } @@ -1654,13 +1639,13 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Response too small"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Response too small"); } else if(rc < 0) { - sftp->read_state = libssh2_NB_state_sent2; + sftp->read_state = ssh2_NB_state_sent2; return rc; } @@ -1673,15 +1658,15 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, case SSH_FXP_STATUS: /* remove the chunk we processed */ - _libssh2_list_remove(&chunk->node); - LIBSSH2_FREE(session, chunk); + ssh2_list_remove(&chunk->node); + SSH2_FREE(session, chunk); /* we must remove all outstanding READ requests, as either we got an error or we are at end of file */ sftp_packetlist_flush(handle); - rc32 = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + rc32 = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(rc32 == LIBSSH2_FX_EOF) { filep->eof = TRUE; @@ -1689,8 +1674,8 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, } else { sftp->last_errno = rc32; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP READ error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP READ error"); } case SSH_FXP_DATA: @@ -1700,21 +1685,21 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, See: https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-02#section-6.4 */ - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Read Packet At Unexpected Offset"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Read Packet At Unexpected Offset"); } - rc32 = _libssh2_ntohu32(data + 5); + rc32 = ssh2_ntohu32(data + 5); if(rc32 > (data_len - 9)) - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol badness"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol badness"); if(rc32 > chunk->len) { /* A chunk larger than we requested was returned to us. This is a protocol violation and we do not know how to deal with it. Bail out! */ - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "FXP_READ response too big"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "FXP_READ response too big"); } if(rc32 != chunk->len) { @@ -1748,13 +1733,13 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, if(filep->data_len == 0) /* free the allocated data if not stored to keep */ - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); /* remove the chunk we processed keeping track of the * next one in case we need it */ - next = _libssh2_list_next(&chunk->node); - _libssh2_list_remove(&chunk->node); - LIBSSH2_FREE(session, chunk); + next = ssh2_list_next(&chunk->node); + ssh2_list_remove(&chunk->node); + SSH2_FREE(session, chunk); /* check if we have space left in the buffer * and either continue to the next chunk or stop @@ -1768,9 +1753,9 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, break; default: - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol badness: unrecognised " - "read request response"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol badness: unrecognised " + "read request response"); } } @@ -1784,8 +1769,8 @@ static ssize_t sftp_read(LIBSSH2_SFTP_HANDLE *handle, } /* we should never reach this point */ - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "sftp_read() internal error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "sftp_read() internal error"); } /* @@ -1820,10 +1805,11 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, uint32_t packet_len = (uint32_t)(handle->handle_len + 13); unsigned char *s, *data; static const unsigned char read_responses[2] = { - SSH_FXP_NAME, SSH_FXP_STATUS }; + SSH_FXP_NAME, SSH_FXP_STATUS + }; ssize_t retcode; - if(sftp->readdir_state == libssh2_NB_state_idle) { + if(sftp->readdir_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; if(handle->u.dir.names_left) { @@ -1841,7 +1827,7 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, if(names_packet_len >= 4) { s = (unsigned char *)handle->u.dir.next_name; - real_filename_len = _libssh2_ntohu32(s); + real_filename_len = ssh2_ntohu32(s); s += 4; names_packet_len -= 4; } @@ -1856,10 +1842,10 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, goto end; } - if(buffer_maxlen >= filename_len && names_packet_len >= - filename_len) { + if(buffer_maxlen >= filename_len && + names_packet_len >= filename_len) { memcpy(buffer, s, filename_len); - buffer[filename_len] = '\0'; /* null-terminate */ + buffer[filename_len] = '\0'; /* null-terminate */ s += real_filename_len; names_packet_len -= real_filename_len; } @@ -1869,7 +1855,7 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, } if(names_packet_len >= 4) { - real_longentry_len = _libssh2_ntohu32(s); + real_longentry_len = ssh2_ntohu32(s); s += 4; names_packet_len -= 4; } @@ -1919,52 +1905,52 @@ static ssize_t sftp_readdir(LIBSSH2_SFTP_HANDLE *handle, char *buffer, handle->u.dir.names_packet_len = names_packet_len; if((--handle->u.dir.names_left) == 0) - LIBSSH2_FREE(session, handle->u.dir.names_packet); + SSH2_FREE(session, handle->u.dir.names_packet); end: - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "libssh2_sftp_readdir_ex() return %lu", - (unsigned long)filename_len)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "libssh2_sftp_readdir_ex() return %lu", + (unsigned long)filename_len)); return (ssize_t)filename_len; } /* Request another entry(entries?) */ - s = sftp->readdir_packet = LIBSSH2_ALLOC(session, packet_len); + s = sftp->readdir_packet = SSH2_ALLOC(session, packet_len); if(!sftp->readdir_packet) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "FXP_READDIR packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "FXP_READDIR packet"); - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_READDIR; sftp->readdir_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->readdir_request_id); - _libssh2_store_str(&s, handle->handle, handle->handle_len); + ssh2_store_u32(&s, sftp->readdir_request_id); + ssh2_store_str(&s, handle->handle, handle->handle_len); - sftp->readdir_state = libssh2_NB_state_created; + sftp->readdir_state = ssh2_NB_state_created; } - if(sftp->readdir_state == libssh2_NB_state_created) { - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Reading entries from directory handle")); - retcode = _libssh2_channel_write(channel, 0, sftp->readdir_packet, - packet_len); + if(sftp->readdir_state == ssh2_NB_state_created) { + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Reading entries from directory handle")); + retcode = ssh2_channel_write(channel, 0, sftp->readdir_packet, + packet_len); if(retcode == LIBSSH2_ERROR_EAGAIN) { return retcode; } else if((ssize_t)packet_len != retcode) { - LIBSSH2_FREE(session, sftp->readdir_packet); + SSH2_FREE(session, sftp->readdir_packet); sftp->readdir_packet = NULL; - sftp->readdir_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "_libssh2_channel_write() failed"); + sftp->readdir_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "ssh2_channel_write() failed"); } - LIBSSH2_FREE(session, sftp->readdir_packet); + SSH2_FREE(session, sftp->readdir_packet); sftp->readdir_packet = NULL; - sftp->readdir_state = libssh2_NB_state_sent; + sftp->readdir_state = ssh2_NB_state_sent; } retcode = sftp_packet_requirev(sftp, 2, read_responses, @@ -1974,40 +1960,39 @@ end: return retcode; else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Status message too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Status message too short"); } else if(retcode) { - sftp->readdir_state = libssh2_NB_state_idle; - return _libssh2_error(session, (int)retcode, - "Timeout waiting for status message"); + sftp->readdir_state = ssh2_NB_state_idle; + return ssh2_err(session, (int)retcode, + "Timeout waiting for status message"); } if(data[0] == SSH_FXP_STATUS) { unsigned int rerrno; - rerrno = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + rerrno = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(rerrno == LIBSSH2_FX_EOF) { - sftp->readdir_state = libssh2_NB_state_idle; + sftp->readdir_state = ssh2_NB_state_idle; return 0; } else { sftp->last_errno = rerrno; - sftp->readdir_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + sftp->readdir_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } - sftp->readdir_state = libssh2_NB_state_idle; + sftp->readdir_state = ssh2_NB_state_idle; - num_names = _libssh2_ntohu32(data + 5); - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "%u entries returned", - num_names)); + num_names = ssh2_ntohu32(data + 5); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "%u entries returned", num_names)); if(!num_names) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); return 0; } @@ -2036,7 +2021,7 @@ int libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *handle, BLOCK_ADJUST(rc, handle->sftp->channel->session, sftp_readdir(handle, buffer, buffer_maxlen, longentry, longentry_maxlen, attrs)); - return (int)rc; /* FIXME: -> ssize_t */ + return (int)rc; /* FIXME: -> ssize_t */ } /* @@ -2094,7 +2079,7 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, switch(sftp->write_state) { default: - case libssh2_NB_state_idle: + case ssh2_NB_state_idle: sftp->last_errno = LIBSSH2_FX_OK; /* Number of bytes sent off that have not been acked and therefore we @@ -2116,42 +2101,42 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, /* there is more data already fine than what we got in this call */ count = 0; - sftp->write_state = libssh2_NB_state_idle; + sftp->write_state = ssh2_NB_state_idle; while(count) { /* TODO: Possibly this should have some logic to prevent a tiny fraction to be left but lets ignore that for now */ uint32_t size = - (uint32_t)(LIBSSH2_MIN(MAX_SFTP_OUTGOING_SIZE, count)); + (uint32_t)(SSH2_MIN(MAX_SFTP_OUTGOING_SIZE, count)); uint32_t request_id; /* 25 = packet_len(4) + packet_type(1) + request_id(4) + handle_len(4) + offset(8) + count(4) */ packet_len = (uint32_t)(handle->handle_len + size + 25); - chunk = LIBSSH2_ALLOC(session, packet_len + - sizeof(struct sftp_pipeline_chunk)); + chunk = SSH2_ALLOC(session, packet_len + + sizeof(struct sftp_pipeline_chunk)); if(!chunk) - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "malloc fail for FXP_WRITE"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "malloc fail for FXP_WRITE"); chunk->len = size; chunk->sent = 0; chunk->lefttosend = packet_len; s = chunk->packet; - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_WRITE; request_id = sftp->request_id++; chunk->request_id = request_id; - _libssh2_store_u32(&s, request_id); - _libssh2_store_str(&s, handle->handle, handle->handle_len); - _libssh2_store_u64(&s, handle->u.file.offset_sent); + ssh2_store_u32(&s, request_id); + ssh2_store_str(&s, handle->handle, handle->handle_len); + ssh2_store_u64(&s, handle->u.file.offset_sent); handle->u.file.offset_sent += size; /* advance offset at once */ - _libssh2_store_str(&s, buffer, size); + ssh2_store_str(&s, buffer, size); /* add this new entry LAST in the list */ - _libssh2_list_add(&handle->packet_list, &chunk->node); + ssh2_list_add(&handle->packet_list, &chunk->node); buffer += size; count -= size; /* deduct the size we used, as we might have @@ -2160,13 +2145,13 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, /* move through the WRITE packets that have not been sent and send as many as possible - remember that we do not block */ - chunk = _libssh2_list_first(&handle->packet_list); + chunk = ssh2_list_first(&handle->packet_list); while(chunk) { if(chunk->lefttosend) { - rc = _libssh2_channel_write(channel, 0, - &chunk->packet[chunk->sent], - chunk->lefttosend); + rc = ssh2_channel_write(channel, 0, + &chunk->packet[chunk->sent], + chunk->lefttosend); if(rc < 0) /* remain in idle state */ return rc; @@ -2181,18 +2166,17 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, } /* move on to the next chunk with data to send */ - chunk = _libssh2_list_next(&chunk->node); + chunk = ssh2_list_next(&chunk->node); } + SSH2_FALLTHROUGH(); - LIBSSH2_FALLTHROUGH(); + case ssh2_NB_state_sent: - case libssh2_NB_state_sent: - - sftp->write_state = libssh2_NB_state_idle; + sftp->write_state = ssh2_NB_state_idle; /* * Count all ACKed packets */ - chunk = _libssh2_list_first(&handle->packet_list); + chunk = ssh2_list_first(&handle->packet_list); while(chunk) { if(chunk->lefttosend) @@ -2210,19 +2194,19 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, chunk->request_id, &data, &data_len, 9); if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "FXP write packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "FXP write packet too short"); } else if(rc < 0) { if(rc == LIBSSH2_ERROR_EAGAIN) - sftp->write_state = libssh2_NB_state_sent; + sftp->write_state = ssh2_NB_state_sent; return rc; } - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); sftp->last_errno = retcode; if(retcode == LIBSSH2_FX_OK) { @@ -2232,10 +2216,10 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, /* we increase the offset value for all acks */ handle->u.file.offset += chunk->len; - next = _libssh2_list_next(&chunk->node); + next = ssh2_list_next(&chunk->node); - _libssh2_list_remove(&chunk->node); /* remove from list */ - LIBSSH2_FREE(session, chunk); /* free memory */ + ssh2_list_remove(&chunk->node); /* remove from list */ + SSH2_FREE(session, chunk); /* free memory */ chunk = next; } @@ -2258,8 +2242,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, /* the server returned an error for that written chunk, propagate this back to our parent function */ - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "FXP write failed"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "FXP write failed"); } } break; @@ -2272,7 +2256,7 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer, acked += handle->u.file.acked; if(acked) { - ssize_t ret = LIBSSH2_MIN(acked, org_count); + ssize_t ret = SSH2_MIN(acked, org_count); /* we got data acked so return that amount, but no more than what was asked to get sent! */ @@ -2315,47 +2299,47 @@ static int sftp_fsync(LIBSSH2_SFTP_HANDLE *handle) ssize_t rc; uint32_t retcode; - if(sftp->fsync_state == libssh2_NB_state_idle) { + if(sftp->fsync_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Issuing fsync command")); - s = packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Issuing fsync command")); + s = packet = SSH2_ALLOC(session, packet_len); if(!packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_EXTENDED " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_EXTENDED " + "packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_EXTENDED; sftp->fsync_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->fsync_request_id); - _libssh2_store_str(&s, "fsync@openssh.com", 17); - _libssh2_store_str(&s, handle->handle, handle->handle_len); + ssh2_store_u32(&s, sftp->fsync_request_id); + ssh2_store_str(&s, "fsync@openssh.com", 17); + ssh2_store_str(&s, handle->handle, handle->handle_len); - sftp->fsync_state = libssh2_NB_state_created; + sftp->fsync_state = ssh2_NB_state_created; } else { packet = sftp->fsync_packet; } - if(sftp->fsync_state == libssh2_NB_state_created) { - rc = _libssh2_channel_write(channel, 0, packet, packet_len); + if(sftp->fsync_state == ssh2_NB_state_created) { + rc = ssh2_channel_write(channel, 0, packet, packet_len); if(rc == LIBSSH2_ERROR_EAGAIN || (0 <= rc && rc < (ssize_t)packet_len)) { sftp->fsync_packet = packet; return LIBSSH2_ERROR_EAGAIN; } - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); sftp->fsync_packet = NULL; if(rc < 0) { - sftp->fsync_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "_libssh2_channel_write() failed"); + sftp->fsync_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "ssh2_channel_write() failed"); } - sftp->fsync_state = libssh2_NB_state_sent; + sftp->fsync_state = ssh2_NB_state_sent; } rc = sftp_packet_require(sftp, SSH_FXP_STATUS, @@ -2365,26 +2349,25 @@ static int sftp_fsync(LIBSSH2_SFTP_HANDLE *handle) } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP fsync packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP fsync packet too short"); } else if(rc) { - sftp->fsync_state = libssh2_NB_state_idle; - return _libssh2_error(session, (int)rc, - "Error waiting for FXP EXTENDED REPLY"); + sftp->fsync_state = ssh2_NB_state_idle; + return ssh2_err(session, (int)rc, + "Error waiting for FXP EXTENDED REPLY"); } - sftp->fsync_state = libssh2_NB_state_idle; + sftp->fsync_state = ssh2_NB_state_idle; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode != LIBSSH2_FX_OK) { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "fsync failed"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, "fsync failed"); } return 0; @@ -2417,53 +2400,53 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle, uint32_t packet_len = (uint32_t)(handle->handle_len + 13 + (setstat ? sftp_attrsize(attrs->flags) : 0)); unsigned char *s, *data = NULL; - static const unsigned char fstat_responses[2] = - { SSH_FXP_ATTRS, SSH_FXP_STATUS }; + static const unsigned char fstat_responses[2] = { + SSH_FXP_ATTRS, SSH_FXP_STATUS + }; ssize_t rc; - if(sftp->fstat_state == libssh2_NB_state_idle) { + if(sftp->fstat_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Issuing %s command", - setstat ? "set-stat" : "stat")); - s = sftp->fstat_packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Issuing %s command", + setstat ? "set-stat" : "stat")); + s = sftp->fstat_packet = SSH2_ALLOC(session, packet_len); if(!sftp->fstat_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "FSTAT/FSETSTAT packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "FSTAT/FSETSTAT packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = setstat ? SSH_FXP_FSETSTAT : SSH_FXP_FSTAT; sftp->fstat_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->fstat_request_id); - _libssh2_store_str(&s, handle->handle, handle->handle_len); + ssh2_store_u32(&s, sftp->fstat_request_id); + ssh2_store_str(&s, handle->handle, handle->handle_len); if(setstat) { s += sftp_attr2bin(s, attrs); } - sftp->fstat_state = libssh2_NB_state_created; + sftp->fstat_state = ssh2_NB_state_created; } - if(sftp->fstat_state == libssh2_NB_state_created) { - rc = _libssh2_channel_write(channel, 0, sftp->fstat_packet, - packet_len); + if(sftp->fstat_state == ssh2_NB_state_created) { + rc = ssh2_channel_write(channel, 0, sftp->fstat_packet, packet_len); if(rc == LIBSSH2_ERROR_EAGAIN) { return (int)rc; } else if((ssize_t)packet_len != rc) { - LIBSSH2_FREE(session, sftp->fstat_packet); + SSH2_FREE(session, sftp->fstat_packet); sftp->fstat_packet = NULL; - sftp->fstat_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - (setstat ? "Unable to send FXP_FSETSTAT" - : "Unable to send FXP_FSTAT command")); + sftp->fstat_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + (setstat ? "Unable to send FXP_FSETSTAT" + : "Unable to send FXP_FSTAT command")); } - LIBSSH2_FREE(session, sftp->fstat_packet); + SSH2_FREE(session, sftp->fstat_packet); sftp->fstat_packet = NULL; - sftp->fstat_state = libssh2_NB_state_sent; + sftp->fstat_state = ssh2_NB_state_sent; } rc = sftp_packet_requirev(sftp, 2, fstat_responses, @@ -2473,41 +2456,41 @@ static int sftp_fstat(LIBSSH2_SFTP_HANDLE *handle, return (int)rc; else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP fstat packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP fstat packet too short"); } else if(rc) { - sftp->fstat_state = libssh2_NB_state_idle; - return _libssh2_error(session, (int)rc, - "Timeout waiting for status message"); + sftp->fstat_state = ssh2_NB_state_idle; + return ssh2_err(session, (int)rc, + "Timeout waiting for status message"); } - sftp->fstat_state = libssh2_NB_state_idle; + sftp->fstat_state = ssh2_NB_state_idle; if(data[0] == SSH_FXP_STATUS) { uint32_t retcode; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode == LIBSSH2_FX_OK) { return 0; } else { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Attributes too short in SFTP fstat"); + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Attributes too short in SFTP fstat"); } - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); return 0; } @@ -2544,7 +2527,7 @@ void libssh2_sftp_seek64(LIBSSH2_SFTP_HANDLE *handle, libssh2_uint64_t offset) /* free the left received buffered data */ if(handle->u.file.data_left) { - LIBSSH2_FREE(handle->sftp->channel->session, handle->u.file.data); + SSH2_FREE(handle->sftp->channel->session, handle->u.file.data); handle->u.file.data_left = handle->u.file.data_len = 0; handle->u.file.data = NULL; } @@ -2596,29 +2579,29 @@ static void sftp_packet_flush(LIBSSH2_SFTP *sftp) { LIBSSH2_CHANNEL *channel = sftp->channel; LIBSSH2_SESSION *session = channel->session; - struct sftp_packet *packet = _libssh2_list_first(&sftp->packets); + struct sftp_packet *packet = ssh2_list_first(&sftp->packets); struct sftp_zombie_requests *zombie = - _libssh2_list_first(&sftp->zombie_requests); + ssh2_list_first(&sftp->zombie_requests); while(packet) { struct sftp_packet *next; /* check next struct in the list */ - next = _libssh2_list_next(&packet->node); - _libssh2_list_remove(&packet->node); - LIBSSH2_FREE(session, packet->data); - LIBSSH2_FREE(session, packet); + next = ssh2_list_next(&packet->node); + ssh2_list_remove(&packet->node); + SSH2_FREE(session, packet->data); + SSH2_FREE(session, packet); packet = next; } while(zombie) { /* figure out the next node */ - struct sftp_zombie_requests *next = _libssh2_list_next(&zombie->node); + struct sftp_zombie_requests *next = ssh2_list_next(&zombie->node); /* unlink the current one */ - _libssh2_list_remove(&zombie->node); + ssh2_list_remove(&zombie->node); /* free the memory */ - LIBSSH2_FREE(session, zombie); + SSH2_FREE(session, zombie); zombie = next; } } @@ -2641,49 +2624,48 @@ static int sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle) unsigned char *s, *data = NULL; int rc = 0; - if(handle->close_state == libssh2_NB_state_idle) { + if(handle->close_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Closing handle")); - s = handle->close_packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Closing handle")); + s = handle->close_packet = SSH2_ALLOC(session, packet_len); if(!handle->close_packet) { - handle->close_state = libssh2_NB_state_idle; - rc = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_CLOSE " - "packet"); + handle->close_state = ssh2_NB_state_idle; + rc = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_CLOSE packet"); } else { - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_CLOSE; handle->close_request_id = sftp->request_id++; - _libssh2_store_u32(&s, handle->close_request_id); - _libssh2_store_str(&s, handle->handle, handle->handle_len); - handle->close_state = libssh2_NB_state_created; + ssh2_store_u32(&s, handle->close_request_id); + ssh2_store_str(&s, handle->handle, handle->handle_len); + handle->close_state = ssh2_NB_state_created; } } - if(handle->close_state == libssh2_NB_state_created) { + if(handle->close_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, handle->close_packet, - packet_len); + nwritten = + ssh2_channel_write(channel, 0, handle->close_packet, packet_len); if(nwritten == LIBSSH2_ERROR_EAGAIN) { return (int)nwritten; } else if((ssize_t)packet_len != nwritten) { - LIBSSH2_FREE(session, handle->close_packet); + SSH2_FREE(session, handle->close_packet); handle->close_packet = NULL; - handle->close_state = libssh2_NB_state_idle; - rc = _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send FXP_CLOSE command"); + handle->close_state = ssh2_NB_state_idle; + rc = ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send FXP_CLOSE command"); } else - handle->close_state = libssh2_NB_state_sent; + handle->close_state = ssh2_NB_state_sent; - LIBSSH2_FREE(session, handle->close_packet); + SSH2_FREE(session, handle->close_packet); handle->close_packet = NULL; } - if(handle->close_state == libssh2_NB_state_sent) { + if(handle->close_state == ssh2_NB_state_sent) { rc = sftp_packet_require(sftp, SSH_FXP_STATUS, handle->close_request_id, &data, &data_len, 9); @@ -2692,17 +2674,17 @@ static int sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle) } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } data = NULL; - _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Packet too short in FXP_CLOSE command"); + ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Packet too short in FXP_CLOSE command"); } else if(rc) { - _libssh2_error(session, rc, "Error waiting for status message"); + ssh2_err(session, rc, "Error waiting for status message"); } - handle->close_state = libssh2_NB_state_sent1; + handle->close_state = ssh2_NB_state_sent1; } if(!data) { @@ -2711,35 +2693,35 @@ static int sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle) assert(rc); } else { - uint32_t retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + uint32_t retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode != LIBSSH2_FX_OK) { sftp->last_errno = retcode; - handle->close_state = libssh2_NB_state_idle; - rc = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + handle->close_state = ssh2_NB_state_idle; + rc = ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } /* remove this handle from the parent's list */ - _libssh2_list_remove(&handle->node); + ssh2_list_remove(&handle->node); if(handle->handle_type == LIBSSH2_SFTP_HANDLE_DIR) { if(handle->u.dir.names_left) - LIBSSH2_FREE(session, handle->u.dir.names_packet); + SSH2_FREE(session, handle->u.dir.names_packet); } else if(handle->handle_type == LIBSSH2_SFTP_HANDLE_FILE) { if(handle->u.file.data) - LIBSSH2_FREE(session, handle->u.file.data); + SSH2_FREE(session, handle->u.file.data); } sftp_packetlist_flush(handle); - sftp->read_state = libssh2_NB_state_idle; + sftp->read_state = ssh2_NB_state_idle; - handle->close_state = libssh2_NB_state_idle; + handle->close_state = ssh2_NB_state_idle; - LIBSSH2_FREE(session, handle); + SSH2_FREE(session, handle); return rc; } @@ -2777,50 +2759,47 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, packet_len = 13; if(packet_len + (uint32_t)filename_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_unlink"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_unlink"); } packet_len += (uint32_t)filename_len; - if(sftp->unlink_state == libssh2_NB_state_idle) { + if(sftp->unlink_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Unlinking %s", filename)); - s = sftp->unlink_packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Unlinking %s", filename)); + s = sftp->unlink_packet = SSH2_ALLOC(session, packet_len); if(!sftp->unlink_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_REMOVE " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_REMOVE packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_REMOVE; sftp->unlink_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->unlink_request_id); - _libssh2_store_str(&s, filename, filename_len); - sftp->unlink_state = libssh2_NB_state_created; + ssh2_store_u32(&s, sftp->unlink_request_id); + ssh2_store_str(&s, filename, filename_len); + sftp->unlink_state = ssh2_NB_state_created; } - if(sftp->unlink_state == libssh2_NB_state_created) { + if(sftp->unlink_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, sftp->unlink_packet, - packet_len); + nwritten = + ssh2_channel_write(channel, 0, sftp->unlink_packet, packet_len); if(nwritten == LIBSSH2_ERROR_EAGAIN) { return (int)nwritten; } else if((ssize_t)packet_len != nwritten) { - LIBSSH2_FREE(session, sftp->unlink_packet); + SSH2_FREE(session, sftp->unlink_packet); sftp->unlink_packet = NULL; - sftp->unlink_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send FXP_REMOVE command"); + sftp->unlink_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send FXP_REMOVE command"); } - LIBSSH2_FREE(session, sftp->unlink_packet); + SSH2_FREE(session, sftp->unlink_packet); sftp->unlink_packet = NULL; - sftp->unlink_state = libssh2_NB_state_sent; + sftp->unlink_state = ssh2_NB_state_sent; } rc = sftp_packet_require(sftp, SSH_FXP_STATUS, @@ -2831,28 +2810,28 @@ static int sftp_unlink(LIBSSH2_SFTP *sftp, } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP unlink packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP unlink packet too short"); } else if(rc) { - sftp->unlink_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Error waiting for FXP STATUS"); + sftp->unlink_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Error waiting for FXP STATUS"); } - sftp->unlink_state = libssh2_NB_state_idle; + sftp->unlink_state = ssh2_NB_state_idle; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode == LIBSSH2_FX_OK) { return 0; } else { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } @@ -2893,68 +2872,63 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, packet_len = 17 + (sftp->version >= 5 ? 4 : 0); if(packet_len + (uint32_t)source_filename_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_rename"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_rename"); } packet_len += (uint32_t)source_filename_len; if(packet_len + (uint32_t)dest_filename_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large (2) " - "sftp_rename"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large (2) sftp_rename"); } packet_len += dest_filename_len; - if(sftp->rename_state == libssh2_NB_state_idle) { + if(sftp->rename_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; if(sftp->version < 2) { - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Server does not support RENAME"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Server does not support RENAME"); } - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Renaming %s to %s", - source_filename, dest_filename)); - sftp->rename_s = sftp->rename_packet = - LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Renaming %s to %s", + source_filename, dest_filename)); + sftp->rename_s = sftp->rename_packet = SSH2_ALLOC(session, packet_len); if(!sftp->rename_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_RENAME " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_RENAME packet"); } - _libssh2_store_u32(&sftp->rename_s, packet_len - 4); + ssh2_store_u32(&sftp->rename_s, packet_len - 4); *(sftp->rename_s++) = SSH_FXP_RENAME; sftp->rename_request_id = sftp->request_id++; - _libssh2_store_u32(&sftp->rename_s, sftp->rename_request_id); - _libssh2_store_str(&sftp->rename_s, source_filename, - source_filename_len); - _libssh2_store_str(&sftp->rename_s, dest_filename, dest_filename_len); + ssh2_store_u32(&sftp->rename_s, sftp->rename_request_id); + ssh2_store_str(&sftp->rename_s, source_filename, source_filename_len); + ssh2_store_str(&sftp->rename_s, dest_filename, dest_filename_len); if(sftp->version >= 5) - _libssh2_store_u32(&sftp->rename_s, (uint32_t)flags); + ssh2_store_u32(&sftp->rename_s, (uint32_t)flags); - sftp->rename_state = libssh2_NB_state_created; + sftp->rename_state = ssh2_NB_state_created; } - if(sftp->rename_state == libssh2_NB_state_created) { - rc = _libssh2_channel_write(channel, 0, sftp->rename_packet, - sftp->rename_s - sftp->rename_packet); + if(sftp->rename_state == ssh2_NB_state_created) { + rc = ssh2_channel_write(channel, 0, sftp->rename_packet, + sftp->rename_s - sftp->rename_packet); if(rc == LIBSSH2_ERROR_EAGAIN) { return (int)rc; } else if((ssize_t)packet_len != rc) { - LIBSSH2_FREE(session, sftp->rename_packet); + SSH2_FREE(session, sftp->rename_packet); sftp->rename_packet = NULL; - sftp->rename_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send FXP_RENAME command"); + sftp->rename_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send FXP_RENAME command"); } - LIBSSH2_FREE(session, sftp->rename_packet); + SSH2_FREE(session, sftp->rename_packet); sftp->rename_packet = NULL; - sftp->rename_state = libssh2_NB_state_sent; + sftp->rename_state = ssh2_NB_state_sent; } rc = sftp_packet_require(sftp, SSH_FXP_STATUS, @@ -2965,21 +2939,20 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP rename packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP rename packet too short"); } else if(rc) { - sftp->rename_state = libssh2_NB_state_idle; - return _libssh2_error(session, (int)rc, - "Error waiting for FXP STATUS"); + sftp->rename_state = ssh2_NB_state_idle; + return ssh2_err(session, (int)rc, "Error waiting for FXP STATUS"); } - sftp->rename_state = libssh2_NB_state_idle; + sftp->rename_state = ssh2_NB_state_idle; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); sftp->last_errno = retcode; @@ -2991,19 +2964,19 @@ static int sftp_rename(LIBSSH2_SFTP *sftp, break; case LIBSSH2_FX_FILE_ALREADY_EXISTS: - retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "File already exists and " - "SSH_FXP_RENAME_OVERWRITE not specified"); + retcode = ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "File already exists and " + "SSH_FXP_RENAME_OVERWRITE not specified"); break; case LIBSSH2_FX_OP_UNSUPPORTED: - retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Operation Not Supported"); + retcode = ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Operation Not Supported"); break; default: - retcode = _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + retcode = ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); break; } @@ -3043,9 +3016,8 @@ static int sftp_posix_rename(LIBSSH2_SFTP *sftp, const char *source_filename, uint32_t retcode; if(sftp->posix_rename_version != 1) { - return _libssh2_error(session, LIBSSH2_FX_OP_UNSUPPORTED, - "Server does not support " - "posix-rename@openssh.com"); + return ssh2_err(session, LIBSSH2_FX_OP_UNSUPPORTED, + "Server does not support posix-rename@openssh.com"); } /* 45 = packet_len(4) + packet_type(1) + request_id(4) + @@ -3056,60 +3028,58 @@ static int sftp_posix_rename(LIBSSH2_SFTP *sftp, const char *source_filename, packet_len = 45; if(packet_len + (uint32_t)source_filename_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "posix-rename@openssh.com"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large posix-rename@openssh.com"); } packet_len += (uint32_t)source_filename_len; if(packet_len + (uint32_t)dest_filename_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large (2) " - "posix-rename@openssh.com"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large (2) posix-rename@openssh.com"); } packet_len += (uint32_t)dest_filename_len; - if(sftp->posix_rename_state == libssh2_NB_state_idle) { - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Issuing posix_rename command")); - s = packet = LIBSSH2_ALLOC(session, packet_len); + if(sftp->posix_rename_state == ssh2_NB_state_idle) { + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Issuing posix_rename command")); + s = packet = SSH2_ALLOC(session, packet_len); if(!packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_EXTENDED " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_EXTENDED " + "packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_EXTENDED; sftp->posix_rename_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->posix_rename_request_id); - _libssh2_store_str(&s, "posix-rename@openssh.com", 24); - _libssh2_store_str(&s, source_filename, source_filename_len); - _libssh2_store_str(&s, dest_filename, dest_filename_len); + ssh2_store_u32(&s, sftp->posix_rename_request_id); + ssh2_store_str(&s, "posix-rename@openssh.com", 24); + ssh2_store_str(&s, source_filename, source_filename_len); + ssh2_store_str(&s, dest_filename, dest_filename_len); - sftp->posix_rename_state = libssh2_NB_state_created; + sftp->posix_rename_state = ssh2_NB_state_created; } else { packet = sftp->posix_rename_packet; } - if(sftp->posix_rename_state == libssh2_NB_state_created) { - rc = _libssh2_channel_write(channel, 0, packet, packet_len); + if(sftp->posix_rename_state == ssh2_NB_state_created) { + rc = ssh2_channel_write(channel, 0, packet, packet_len); if(rc == LIBSSH2_ERROR_EAGAIN || (0 <= rc && rc < (ssize_t)packet_len)) { sftp->posix_rename_packet = packet; return LIBSSH2_ERROR_EAGAIN; } - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); sftp->posix_rename_packet = NULL; if(rc < 0) { - sftp->posix_rename_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "_libssh2_channel_write() failed"); + sftp->posix_rename_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "ssh2_channel_write() failed"); } - sftp->posix_rename_state = libssh2_NB_state_sent; + sftp->posix_rename_state = ssh2_NB_state_sent; } rc = sftp_packet_require(sftp, SSH_FXP_STATUS, @@ -3120,26 +3090,26 @@ static int sftp_posix_rename(LIBSSH2_SFTP *sftp, const char *source_filename, } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP posix_rename packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP posix_rename packet too short"); } else if(rc) { - sftp->posix_rename_state = libssh2_NB_state_idle; - return (int)_libssh2_error(session, (int)rc, - "Error waiting for FXP EXTENDED REPLY"); + sftp->posix_rename_state = ssh2_NB_state_idle; + return (int)ssh2_err(session, (int)rc, + "Error waiting for FXP EXTENDED REPLY"); } - sftp->posix_rename_state = libssh2_NB_state_idle; + sftp->posix_rename_state = ssh2_NB_state_idle; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode != LIBSSH2_FX_OK) { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "posix_rename failed"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "posix_rename failed"); } return 0; @@ -3178,8 +3148,9 @@ static int sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st) unsigned char *packet, *s, *data = NULL; ssize_t rc; unsigned int flag; - static const unsigned char responses[2] = - { SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS }; + static const unsigned char responses[2] = { + SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS + }; /* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4) + handle_len (4) */ @@ -3187,54 +3158,53 @@ static int sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st) packet_len = 20 + 17; if(packet_len + (uint32_t)handle->handle_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_fstatvfs"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_fstatvfs"); } packet_len += (uint32_t)handle->handle_len; - if(sftp->fstatvfs_state == libssh2_NB_state_idle) { + if(sftp->fstatvfs_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Getting file system statistics")); - s = packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Getting file system statistics")); + s = packet = SSH2_ALLOC(session, packet_len); if(!packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_EXTENDED " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_EXTENDED " + "packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_EXTENDED; sftp->fstatvfs_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->fstatvfs_request_id); - _libssh2_store_str(&s, "fstatvfs@openssh.com", 20); - _libssh2_store_str(&s, handle->handle, handle->handle_len); + ssh2_store_u32(&s, sftp->fstatvfs_request_id); + ssh2_store_str(&s, "fstatvfs@openssh.com", 20); + ssh2_store_str(&s, handle->handle, handle->handle_len); - sftp->fstatvfs_state = libssh2_NB_state_created; + sftp->fstatvfs_state = ssh2_NB_state_created; } else { packet = sftp->fstatvfs_packet; } - if(sftp->fstatvfs_state == libssh2_NB_state_created) { - rc = _libssh2_channel_write(channel, 0, packet, packet_len); + if(sftp->fstatvfs_state == ssh2_NB_state_created) { + rc = ssh2_channel_write(channel, 0, packet, packet_len); if(rc == LIBSSH2_ERROR_EAGAIN || (0 <= rc && rc < (ssize_t)packet_len)) { sftp->fstatvfs_packet = packet; return LIBSSH2_ERROR_EAGAIN; } - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); sftp->fstatvfs_packet = NULL; if(rc < 0) { - sftp->fstatvfs_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "_libssh2_channel_write() failed"); + sftp->fstatvfs_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "ssh2_channel_write() failed"); } - sftp->fstatvfs_state = libssh2_NB_state_sent; + sftp->fstatvfs_state = ssh2_NB_state_sent; } rc = sftp_packet_requirev(sftp, 2, responses, sftp->fstatvfs_request_id, @@ -3245,53 +3215,53 @@ static int sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle, LIBSSH2_SFTP_STATVFS *st) } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP rename packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP rename packet too short"); } else if(rc) { - sftp->fstatvfs_state = libssh2_NB_state_idle; - return _libssh2_error(session, (int)rc, - "Error waiting for FXP EXTENDED REPLY"); + sftp->fstatvfs_state = ssh2_NB_state_idle; + return ssh2_err(session, (int)rc, + "Error waiting for FXP EXTENDED REPLY"); } if(data[0] == SSH_FXP_STATUS) { - uint32_t retcode = _libssh2_ntohu32(data + 5); - sftp->fstatvfs_state = libssh2_NB_state_idle; - LIBSSH2_FREE(session, data); + uint32_t retcode = ssh2_ntohu32(data + 5); + sftp->fstatvfs_state = ssh2_NB_state_idle; + SSH2_FREE(session, data); sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } if(data_len < 93) { - LIBSSH2_FREE(session, data); - sftp->fstatvfs_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error: short response"); + SSH2_FREE(session, data); + sftp->fstatvfs_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error: short response"); } - sftp->fstatvfs_state = libssh2_NB_state_idle; + sftp->fstatvfs_state = ssh2_NB_state_idle; - st->f_bsize = _libssh2_ntohu64(data + 5); - st->f_frsize = _libssh2_ntohu64(data + 13); - st->f_blocks = _libssh2_ntohu64(data + 21); - st->f_bfree = _libssh2_ntohu64(data + 29); - st->f_bavail = _libssh2_ntohu64(data + 37); - st->f_files = _libssh2_ntohu64(data + 45); - st->f_ffree = _libssh2_ntohu64(data + 53); - st->f_favail = _libssh2_ntohu64(data + 61); - st->f_fsid = _libssh2_ntohu64(data + 69); - flag = (unsigned int)_libssh2_ntohu64(data + 77); - st->f_namemax = _libssh2_ntohu64(data + 85); + st->f_bsize = ssh2_ntohu64(data + 5); + st->f_frsize = ssh2_ntohu64(data + 13); + st->f_blocks = ssh2_ntohu64(data + 21); + st->f_bfree = ssh2_ntohu64(data + 29); + st->f_bavail = ssh2_ntohu64(data + 37); + st->f_files = ssh2_ntohu64(data + 45); + st->f_ffree = ssh2_ntohu64(data + 53); + st->f_favail = ssh2_ntohu64(data + 61); + st->f_fsid = ssh2_ntohu64(data + 69); + flag = (unsigned int)ssh2_ntohu64(data + 77); + st->f_namemax = ssh2_ntohu64(data + 85); st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY) ? LIBSSH2_SFTP_ST_RDONLY : 0; st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID) ? LIBSSH2_SFTP_ST_NOSUID : 0; - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); return 0; } @@ -3325,8 +3295,9 @@ static int sftp_statvfs(LIBSSH2_SFTP *sftp, unsigned char *packet, *s, *data = NULL; ssize_t rc; unsigned int flag; - static const unsigned char responses[2] = - { SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS }; + static const unsigned char responses[2] = { + SSH_FXP_EXTENDED_REPLY, SSH_FXP_STATUS + }; /* 17 = packet_len(4) + packet_type(1) + request_id(4) + ext_len(4) + path_len (4) */ @@ -3334,54 +3305,53 @@ static int sftp_statvfs(LIBSSH2_SFTP *sftp, packet_len = 19 + 17; if(packet_len + (uint32_t)path_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_statvfs"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_statvfs"); } packet_len += (uint32_t)path_len; - if(sftp->statvfs_state == libssh2_NB_state_idle) { + if(sftp->statvfs_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Getting file system statistics of %s", path)); - s = packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Getting file system statistics of %s", path)); + s = packet = SSH2_ALLOC(session, packet_len); if(!packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_EXTENDED " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_EXTENDED " + "packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_EXTENDED; sftp->statvfs_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->statvfs_request_id); - _libssh2_store_str(&s, "statvfs@openssh.com", 19); - _libssh2_store_str(&s, path, path_len); + ssh2_store_u32(&s, sftp->statvfs_request_id); + ssh2_store_str(&s, "statvfs@openssh.com", 19); + ssh2_store_str(&s, path, path_len); - sftp->statvfs_state = libssh2_NB_state_created; + sftp->statvfs_state = ssh2_NB_state_created; } else { packet = sftp->statvfs_packet; } - if(sftp->statvfs_state == libssh2_NB_state_created) { - rc = _libssh2_channel_write(channel, 0, packet, packet_len); + if(sftp->statvfs_state == ssh2_NB_state_created) { + rc = ssh2_channel_write(channel, 0, packet, packet_len); if(rc == LIBSSH2_ERROR_EAGAIN || (0 <= rc && rc < (ssize_t)packet_len)) { sftp->statvfs_packet = packet; return LIBSSH2_ERROR_EAGAIN; } - LIBSSH2_FREE(session, packet); + SSH2_FREE(session, packet); sftp->statvfs_packet = NULL; if(rc < 0) { - sftp->statvfs_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "_libssh2_channel_write() failed"); + sftp->statvfs_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "ssh2_channel_write() failed"); } - sftp->statvfs_state = libssh2_NB_state_sent; + sftp->statvfs_state = ssh2_NB_state_sent; } rc = sftp_packet_requirev(sftp, 2, responses, sftp->statvfs_request_id, @@ -3391,53 +3361,53 @@ static int sftp_statvfs(LIBSSH2_SFTP *sftp, } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP fstat packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP fstat packet too short"); } else if(rc) { - sftp->statvfs_state = libssh2_NB_state_idle; - return _libssh2_error(session, (int)rc, - "Error waiting for FXP EXTENDED REPLY"); + sftp->statvfs_state = ssh2_NB_state_idle; + return ssh2_err(session, (int)rc, + "Error waiting for FXP EXTENDED REPLY"); } if(data[0] == SSH_FXP_STATUS) { - uint32_t retcode = _libssh2_ntohu32(data + 5); - sftp->statvfs_state = libssh2_NB_state_idle; - LIBSSH2_FREE(session, data); + uint32_t retcode = ssh2_ntohu32(data + 5); + sftp->statvfs_state = ssh2_NB_state_idle; + SSH2_FREE(session, data); sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } if(data_len < 93) { - LIBSSH2_FREE(session, data); - sftp->statvfs_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error: short response"); + SSH2_FREE(session, data); + sftp->statvfs_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error: short response"); } - sftp->statvfs_state = libssh2_NB_state_idle; + sftp->statvfs_state = ssh2_NB_state_idle; - st->f_bsize = _libssh2_ntohu64(data + 5); - st->f_frsize = _libssh2_ntohu64(data + 13); - st->f_blocks = _libssh2_ntohu64(data + 21); - st->f_bfree = _libssh2_ntohu64(data + 29); - st->f_bavail = _libssh2_ntohu64(data + 37); - st->f_files = _libssh2_ntohu64(data + 45); - st->f_ffree = _libssh2_ntohu64(data + 53); - st->f_favail = _libssh2_ntohu64(data + 61); - st->f_fsid = _libssh2_ntohu64(data + 69); - flag = (unsigned int)_libssh2_ntohu64(data + 77); - st->f_namemax = _libssh2_ntohu64(data + 85); + st->f_bsize = ssh2_ntohu64(data + 5); + st->f_frsize = ssh2_ntohu64(data + 13); + st->f_blocks = ssh2_ntohu64(data + 21); + st->f_bfree = ssh2_ntohu64(data + 29); + st->f_bavail = ssh2_ntohu64(data + 37); + st->f_files = ssh2_ntohu64(data + 45); + st->f_ffree = ssh2_ntohu64(data + 53); + st->f_favail = ssh2_ntohu64(data + 61); + st->f_fsid = ssh2_ntohu64(data + 69); + flag = (unsigned int)ssh2_ntohu64(data + 77); + st->f_namemax = ssh2_ntohu64(data + 85); st->f_flag = (flag & SSH_FXE_STATVFS_ST_RDONLY) ? LIBSSH2_SFTP_ST_RDONLY : 0; st->f_flag |= (flag & SSH_FXE_STATVFS_ST_NOSUID) ? LIBSSH2_SFTP_ST_NOSUID : 0; - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); return 0; } @@ -3478,60 +3448,58 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, if(mode != LIBSSH2_SFTP_DEFAULT_MODE) { /* Filetype in SFTP 3 and earlier */ attrs.flags = LIBSSH2_SFTP_ATTR_PERMISSIONS; - attrs.permissions = mode | LIBSSH2_SFTP_ATTR_PFILETYPE_DIR; + attrs.permissions = mode | SSH2_SFTP_ATTR_PFILETYPE_DIR; } /* 13 = packet_len(4) + packet_type(1) + request_id(4) + path_len(4) */ packet_len = 13 + sftp_attrsize(attrs.flags); if(packet_len + (uint32_t)path_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_mkdir"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_mkdir"); } packet_len += (uint32_t)path_len; - if(sftp->mkdir_state == libssh2_NB_state_idle) { + if(sftp->mkdir_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, - "Creating directory %s with mode 0%lo", path, mode)); - s = packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, + "Creating directory %s with mode 0%lo", path, mode)); + s = packet = SSH2_ALLOC(session, packet_len); if(!packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_MKDIR " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_MKDIR packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_MKDIR; sftp->mkdir_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->mkdir_request_id); - _libssh2_store_str(&s, path, path_len); + ssh2_store_u32(&s, sftp->mkdir_request_id); + ssh2_store_str(&s, path, path_len); s += sftp_attr2bin(s, &attrs); - sftp->mkdir_state = libssh2_NB_state_created; + sftp->mkdir_state = ssh2_NB_state_created; } else { packet = sftp->mkdir_packet; } - if(sftp->mkdir_state == libssh2_NB_state_created) { + if(sftp->mkdir_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, packet, packet_len); + nwritten = ssh2_channel_write(channel, 0, packet, packet_len); if(nwritten == LIBSSH2_ERROR_EAGAIN) { sftp->mkdir_packet = packet; return (int)nwritten; } if((ssize_t)packet_len != nwritten) { - LIBSSH2_FREE(session, packet); - sftp->mkdir_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "_libssh2_channel_write() failed"); + SSH2_FREE(session, packet); + sftp->mkdir_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "ssh2_channel_write() failed"); } - LIBSSH2_FREE(session, packet); - sftp->mkdir_state = libssh2_NB_state_sent; + SSH2_FREE(session, packet); + sftp->mkdir_state = ssh2_NB_state_sent; sftp->mkdir_packet = NULL; } @@ -3542,29 +3510,29 @@ static int sftp_mkdir(LIBSSH2_SFTP *sftp, } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP mkdir packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP mkdir packet too short"); } else if(rc) { - sftp->mkdir_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Error waiting for FXP STATUS"); + sftp->mkdir_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Error waiting for FXP STATUS"); } - sftp->mkdir_state = libssh2_NB_state_idle; + sftp->mkdir_state = ssh2_NB_state_idle; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode == LIBSSH2_FX_OK) { - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "OK")); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "OK")); return 0; } else { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } @@ -3599,51 +3567,49 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path, int rc; if(packet_len + (uint32_t)path_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_rmdir"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_rmdir"); } packet_len += (uint32_t)path_len; - if(sftp->rmdir_state == libssh2_NB_state_idle) { + if(sftp->rmdir_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "Removing directory: %s", - path)); - s = sftp->rmdir_packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "Removing directory: %s", + path)); + s = sftp->rmdir_packet = SSH2_ALLOC(session, packet_len); if(!sftp->rmdir_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_RMDIR " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_RMDIR packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); *(s++) = SSH_FXP_RMDIR; sftp->rmdir_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->rmdir_request_id); - _libssh2_store_str(&s, path, path_len); + ssh2_store_u32(&s, sftp->rmdir_request_id); + ssh2_store_str(&s, path, path_len); - sftp->rmdir_state = libssh2_NB_state_created; + sftp->rmdir_state = ssh2_NB_state_created; } - if(sftp->rmdir_state == libssh2_NB_state_created) { + if(sftp->rmdir_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, sftp->rmdir_packet, - packet_len); + nwritten = ssh2_channel_write(channel, 0, sftp->rmdir_packet, + packet_len); if(nwritten == LIBSSH2_ERROR_EAGAIN) { return (int)nwritten; } else if((ssize_t)packet_len != nwritten) { - LIBSSH2_FREE(session, sftp->rmdir_packet); + SSH2_FREE(session, sftp->rmdir_packet); sftp->rmdir_packet = NULL; - sftp->rmdir_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send FXP_RMDIR command"); + sftp->rmdir_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send FXP_RMDIR command"); } - LIBSSH2_FREE(session, sftp->rmdir_packet); + SSH2_FREE(session, sftp->rmdir_packet); sftp->rmdir_packet = NULL; - sftp->rmdir_state = libssh2_NB_state_sent; + sftp->rmdir_state = ssh2_NB_state_sent; } rc = sftp_packet_require(sftp, SSH_FXP_STATUS, @@ -3653,28 +3619,28 @@ static int sftp_rmdir(LIBSSH2_SFTP *sftp, const char *path, } else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP rmdir packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP rmdir packet too short"); } else if(rc) { - sftp->rmdir_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, "Error waiting for FXP STATUS"); + sftp->rmdir_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Error waiting for FXP STATUS"); } - sftp->rmdir_state = libssh2_NB_state_idle; + sftp->rmdir_state = ssh2_NB_state_idle; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode == LIBSSH2_FX_OK) { return 0; } else { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } @@ -3707,33 +3673,31 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, ((stat_type == LIBSSH2_SFTP_SETSTAT) ? sftp_attrsize(attrs->flags) : 0); unsigned char *s, *data = NULL; - static const unsigned char stat_responses[2] = - { SSH_FXP_ATTRS, SSH_FXP_STATUS }; + static const unsigned char stat_responses[2] = { + SSH_FXP_ATTRS, SSH_FXP_STATUS + }; int rc; if(packet_len + (uint32_t)path_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_stat"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_stat"); } packet_len += (uint32_t)path_len; - if(sftp->stat_state == libssh2_NB_state_idle) { + if(sftp->stat_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "%s %s", - (stat_type == LIBSSH2_SFTP_SETSTAT) ? "Set-statting" : - (stat_type == - LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), - path)); - s = sftp->stat_packet = LIBSSH2_ALLOC(session, packet_len); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "%s %s", + (stat_type == LIBSSH2_SFTP_SETSTAT) ? "Set-statting" : + (stat_type == LIBSSH2_SFTP_LSTAT ? "LStatting" : "Statting"), + path)); + s = sftp->stat_packet = SSH2_ALLOC(session, packet_len); if(!sftp->stat_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for FXP_*STAT " - "packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for FXP_*STAT packet"); } - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); switch(stat_type) { case LIBSSH2_SFTP_SETSTAT: @@ -3749,33 +3713,33 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, *(s++) = SSH_FXP_STAT; } sftp->stat_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->stat_request_id); - _libssh2_store_str(&s, path, path_len); + ssh2_store_u32(&s, sftp->stat_request_id); + ssh2_store_str(&s, path, path_len); if(stat_type == LIBSSH2_SFTP_SETSTAT) s += sftp_attr2bin(s, attrs); - sftp->stat_state = libssh2_NB_state_created; + sftp->stat_state = ssh2_NB_state_created; } - if(sftp->stat_state == libssh2_NB_state_created) { + if(sftp->stat_state == ssh2_NB_state_created) { ssize_t nwritten; - nwritten = _libssh2_channel_write(channel, 0, - sftp->stat_packet, packet_len); + nwritten = ssh2_channel_write(channel, 0, + sftp->stat_packet, packet_len); if(nwritten == LIBSSH2_ERROR_EAGAIN) { return (int)nwritten; } else if((ssize_t)packet_len != nwritten) { - LIBSSH2_FREE(session, sftp->stat_packet); + SSH2_FREE(session, sftp->stat_packet); sftp->stat_packet = NULL; - sftp->stat_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send STAT/LSTAT/SETSTAT command"); + sftp->stat_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send STAT/LSTAT/SETSTAT command"); } - LIBSSH2_FREE(session, sftp->stat_packet); + SSH2_FREE(session, sftp->stat_packet); sftp->stat_packet = NULL; - sftp->stat_state = libssh2_NB_state_sent; + sftp->stat_state = ssh2_NB_state_sent; } rc = sftp_packet_requirev(sftp, 2, stat_responses, @@ -3784,43 +3748,42 @@ static int sftp_stat(LIBSSH2_SFTP *sftp, return rc; else if(rc == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP stat packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP stat packet too short"); } else if(rc) { - sftp->stat_state = libssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Timeout waiting for status message"); + sftp->stat_state = ssh2_NB_state_idle; + return ssh2_err(session, rc, "Timeout waiting for status message"); } - sftp->stat_state = libssh2_NB_state_idle; + sftp->stat_state = ssh2_NB_state_idle; if(data[0] == SSH_FXP_STATUS) { uint32_t retcode; - retcode = _libssh2_ntohu32(data + 5); - LIBSSH2_FREE(session, data); + retcode = ssh2_ntohu32(data + 5); + SSH2_FREE(session, data); if(retcode == LIBSSH2_FX_OK) { memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES)); return 0; } else { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } memset(attrs, 0, sizeof(LIBSSH2_SFTP_ATTRIBUTES)); if(sftp_bin2attr(attrs, data + 5, data_len - 5) < 0) { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Attributes too short in SFTP fstat"); + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Attributes too short in SFTP fstat"); } - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); return 0; } @@ -3855,8 +3818,9 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, uint32_t packet_len; unsigned char *s, *data = NULL; struct string_buf buf; - static const unsigned char link_responses[2] = - { SSH_FXP_NAME, SSH_FXP_STATUS }; + static const unsigned char link_responses[2] = { + SSH_FXP_NAME, SSH_FXP_STATUS + }; int retcode; unsigned char packet_type; uint32_t tmp_u32; @@ -3866,9 +3830,8 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, packet_len = 13; if(packet_len + (uint32_t)path_len < packet_len) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large " - "sftp_symlink"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large sftp_symlink"); } packet_len += (uint32_t)path_len; @@ -3876,39 +3839,37 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, if((target_len + 4 < target_len) || (packet_len + (4 + target_len) < packet_len)) { - return _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Input too large (2)" - "sftp_symlink"); + return ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Input too large (2) sftp_symlink"); } else { packet_len += (4 + target_len); } } - if(sftp->symlink_state == libssh2_NB_state_idle) { + if(sftp->symlink_state == ssh2_NB_state_idle) { sftp->last_errno = LIBSSH2_FX_OK; if((sftp->version < 3) && (link_type != LIBSSH2_SFTP_REALPATH)) { - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Server does not support SYMLINK or" - " READLINK"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Server does not support SYMLINK or READLINK"); } - s = sftp->symlink_packet = LIBSSH2_ALLOC(session, packet_len); + s = sftp->symlink_packet = SSH2_ALLOC(session, packet_len); if(!sftp->symlink_packet) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "SYMLINK/READLINK/REALPATH packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "SYMLINK/READLINK/REALPATH packet"); } - _libssh2_debug((session, LIBSSH2_TRACE_SFTP, "%s %s on %s", - (link_type == - LIBSSH2_SFTP_SYMLINK) ? "Creating" : "Reading", - (link_type == - LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", - path)); + ssh2_deb((session, LIBSSH2_TRACE_SFTP, "%s %s on %s", + (link_type == + LIBSSH2_SFTP_SYMLINK) ? "Creating" : "Reading", + (link_type == + LIBSSH2_SFTP_REALPATH) ? "realpath" : "symlink", + path)); - _libssh2_store_u32(&s, packet_len - 4); + ssh2_store_u32(&s, packet_len - 4); switch(link_type) { case LIBSSH2_SFTP_REALPATH: @@ -3924,31 +3885,31 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, *(s++) = SSH_FXP_READLINK; } sftp->symlink_request_id = sftp->request_id++; - _libssh2_store_u32(&s, sftp->symlink_request_id); - _libssh2_store_str(&s, path, path_len); + ssh2_store_u32(&s, sftp->symlink_request_id); + ssh2_store_str(&s, path, path_len); if(link_type == LIBSSH2_SFTP_SYMLINK) - _libssh2_store_str(&s, target, target_len); + ssh2_store_str(&s, target, target_len); - sftp->symlink_state = libssh2_NB_state_created; + sftp->symlink_state = ssh2_NB_state_created; } - if(sftp->symlink_state == libssh2_NB_state_created) { - ssize_t rc = _libssh2_channel_write(channel, 0, sftp->symlink_packet, - packet_len); + if(sftp->symlink_state == ssh2_NB_state_created) { + ssize_t rc = ssh2_channel_write(channel, 0, sftp->symlink_packet, + packet_len); if(rc == LIBSSH2_ERROR_EAGAIN) return (int)rc; else if((ssize_t)packet_len != rc) { - LIBSSH2_FREE(session, sftp->symlink_packet); + SSH2_FREE(session, sftp->symlink_packet); sftp->symlink_packet = NULL; - sftp->symlink_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send SYMLINK/READLINK command"); + sftp->symlink_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send SYMLINK/READLINK command"); } - LIBSSH2_FREE(session, sftp->symlink_packet); + SSH2_FREE(session, sftp->symlink_packet); sftp->symlink_packet = NULL; - sftp->symlink_state = libssh2_NB_state_sent; + sftp->symlink_state = ssh2_NB_state_sent; } retcode = sftp_packet_requirev(sftp, 2, link_responses, @@ -3958,64 +3919,62 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, return retcode; else if(retcode == LIBSSH2_ERROR_BUFFER_TOO_SMALL) { if(data_len > 0) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP symlink packet too short"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP symlink packet too short"); } else if(retcode) { - sftp->symlink_state = libssh2_NB_state_idle; - return _libssh2_error(session, retcode, - "Error waiting for status message"); + sftp->symlink_state = ssh2_NB_state_idle; + return ssh2_err(session, retcode, "Error waiting for status message"); } - sftp->symlink_state = libssh2_NB_state_idle; + sftp->symlink_state = ssh2_NB_state_idle; - buf.data = (unsigned char *)LIBSSH2_UNCONST(data); + buf.data = (unsigned char *)SSH2_UNCONST(data); buf.dataptr = buf.data; buf.len = data_len; - if(_libssh2_get_byte(&buf, &packet_type)) { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error (type)"); + if(ssh2_get_byte(&buf, &packet_type)) { + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error (type)"); } /* advance past id */ - if(_libssh2_get_u32(&buf, &tmp_u32)) { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error (id)"); + if(ssh2_get_u32(&buf, &tmp_u32)) { + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error (id)"); } if(packet_type == SSH_FXP_STATUS) { - if(_libssh2_get_u32(&buf, &tmp_u32)) { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error (code)"); + if(ssh2_get_u32(&buf, &tmp_u32)) { + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error (code)"); } retcode = (int)tmp_u32; - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); if(retcode == LIBSSH2_FX_OK) return LIBSSH2_ERROR_NONE; else { sftp->last_errno = retcode; - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error"); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error"); } } /* look for at least one link */ - if(_libssh2_get_u32(&buf, &tmp_u32) || tmp_u32 < 1) { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "Invalid READLINK/REALPATH response, " - "no name entries"); + if(ssh2_get_u32(&buf, &tmp_u32) || tmp_u32 < 1) { + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "Invalid READLINK/REALPATH response, no name entries"); } - if(_libssh2_get_string(&buf, &lk_target, &lk_len) == LIBSSH2_ERROR_NONE) { + if(ssh2_get_string(&buf, &lk_target, &lk_len) == LIBSSH2_ERROR_NONE) { if(lk_len < target_len) { memcpy(target, lk_target, lk_len); target[lk_len] = '\0'; @@ -4026,12 +3985,12 @@ static int sftp_symlink(LIBSSH2_SFTP *sftp, } } else { - LIBSSH2_FREE(session, data); - return _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL, - "SFTP Protocol Error (filename)"); + SSH2_FREE(session, data); + return ssh2_err(session, LIBSSH2_ERROR_SFTP_PROTOCOL, + "SFTP Protocol Error (filename)"); } - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); return retcode; } diff --git a/src/sftp.h b/src/sftp.h index 2f29d543..0c4cb554 100644 --- a/src/sftp.h +++ b/src/sftp.h @@ -91,8 +91,8 @@ struct _LIBSSH2_SFTP_HANDLE { LIBSSH2_SFTP_HANDLE_DIR } handle_type; - union libssh2_sftp_handle_data { - struct libssh2_sftp_handle_file_data { + union ssh2_sftp_handle_data { + struct ssh2_sftp_handle_file_data { libssh2_uint64_t offset; libssh2_uint64_t offset_sent; size_t acked; /* container for acked data that has not been @@ -109,7 +109,7 @@ struct _LIBSSH2_SFTP_HANDLE { char eof; /* we have read to the end */ } file; - struct libssh2_sftp_handle_dir_data { + struct ssh2_sftp_handle_dir_data { uint32_t names_left; void *names_packet; char *next_name; @@ -118,7 +118,7 @@ struct _LIBSSH2_SFTP_HANDLE { } u; /* State variables used in libssh2_sftp_close_handle() */ - libssh2_nonblocking_states close_state; + ssh2_NB_states close_state; uint32_t close_request_id; unsigned char *close_packet; @@ -141,7 +141,7 @@ struct _LIBSSH2_SFTP { uint32_t last_errno; - /* Holder for partial packet, use in libssh2_sftp_packet_read() */ + /* Holder for partial packet, use in sftp_packet_read() */ unsigned char packet_header[9]; /* packet size (4) packet type (1) request id (4) */ size_t packet_header_len; /* packet_header length */ @@ -149,83 +149,83 @@ struct _LIBSSH2_SFTP { uint32_t partial_len; /* Desired number of bytes */ size_t partial_received; /* Bytes received so far */ - /* Time that libssh2_sftp_packet_requirev() started reading */ + /* Time that sftp_packet_requirev() started reading */ time_t requirev_start; /* State variables used in libssh2_sftp_open_ex() */ - libssh2_nonblocking_states open_state; + ssh2_NB_states open_state; unsigned char *open_packet; uint32_t open_packet_len; /* 32-bit on the wire */ size_t open_packet_sent; uint32_t open_request_id; /* State variable used in sftp_read() */ - libssh2_nonblocking_states read_state; + ssh2_NB_states read_state; /* State variable used in sftp_packet_read() */ - libssh2_nonblocking_states packet_state; + ssh2_NB_states packet_state; /* State variable used in sftp_write() */ - libssh2_nonblocking_states write_state; + ssh2_NB_states write_state; /* State variables used in sftp_fsync() */ - libssh2_nonblocking_states fsync_state; + ssh2_NB_states fsync_state; unsigned char *fsync_packet; uint32_t fsync_request_id; /* State variables used in libssh2_sftp_readdir() */ - libssh2_nonblocking_states readdir_state; + ssh2_NB_states readdir_state; unsigned char *readdir_packet; uint32_t readdir_request_id; /* State variables used in libssh2_sftp_fstat_ex() */ - libssh2_nonblocking_states fstat_state; + ssh2_NB_states fstat_state; unsigned char *fstat_packet; uint32_t fstat_request_id; /* State variables used in libssh2_sftp_unlink_ex() */ - libssh2_nonblocking_states unlink_state; + ssh2_NB_states unlink_state; unsigned char *unlink_packet; uint32_t unlink_request_id; /* State variables used in libssh2_sftp_rename_ex() */ - libssh2_nonblocking_states rename_state; + ssh2_NB_states rename_state; unsigned char *rename_packet; unsigned char *rename_s; uint32_t rename_request_id; /* State variables used in libssh2_sftp_posix_rename_ex() */ - libssh2_nonblocking_states posix_rename_state; + ssh2_NB_states posix_rename_state; unsigned char *posix_rename_packet; uint32_t posix_rename_request_id; /* State variables used in libssh2_sftp_fstatvfs() */ - libssh2_nonblocking_states fstatvfs_state; + ssh2_NB_states fstatvfs_state; unsigned char *fstatvfs_packet; uint32_t fstatvfs_request_id; /* State variables used in libssh2_sftp_statvfs() */ - libssh2_nonblocking_states statvfs_state; + ssh2_NB_states statvfs_state; unsigned char *statvfs_packet; uint32_t statvfs_request_id; /* State variables used in libssh2_sftp_mkdir() */ - libssh2_nonblocking_states mkdir_state; + ssh2_NB_states mkdir_state; unsigned char *mkdir_packet; uint32_t mkdir_request_id; /* State variables used in libssh2_sftp_rmdir() */ - libssh2_nonblocking_states rmdir_state; + ssh2_NB_states rmdir_state; unsigned char *rmdir_packet; uint32_t rmdir_request_id; /* State variables used in libssh2_sftp_stat() */ - libssh2_nonblocking_states stat_state; + ssh2_NB_states stat_state; unsigned char *stat_packet; uint32_t stat_request_id; /* State variables used in libssh2_sftp_symlink() */ - libssh2_nonblocking_states symlink_state; + ssh2_NB_states symlink_state; unsigned char *symlink_packet; uint32_t symlink_request_id; }; diff --git a/src/transport.c b/src/transport.c index 8ae661ff..0cc9d5fe 100644 --- a/src/transport.c +++ b/src/transport.c @@ -135,7 +135,7 @@ static int decrypt(LIBSSH2_SESSION *session, unsigned char *source, while(len > 0) { /* normally decrypt up to blocksize bytes at a time */ - ssize_t decryptlen = LIBSSH2_MIN(blocksize, len); + ssize_t decryptlen = SSH2_MIN(blocksize, len); /* The first block is special (since it needs to be decoded to get the length of the remainder of the block) and takes priority. When the length finally gets to the last blocksize bytes, and there is no @@ -154,7 +154,7 @@ static int decrypt(LIBSSH2_SESSION *session, unsigned char *source, if(session->remote.crypt->crypt(session, 0, source, decryptlen, &session->remote.crypt_abstract, lowerfirstlast)) { - LIBSSH2_FREE(session, p->payload); + SSH2_FREE(session, p->payload); p->payload = NULL; return LIBSSH2_ERROR_DECRYPT; } @@ -191,8 +191,8 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) remote_mac = session->remote.mac; } - if(session->fullpacket_state == libssh2_NB_state_idle) { - session->fullpacket_macstate = LIBSSH2_MAC_CONFIRMED; + if(session->fullpacket_state == ssh2_NB_state_idle) { + session->fullpacket_macstate = SSH2_MAC_CONFIRMED; session->fullpacket_payload_len = p->packet_length - 1; if(encrypted && remote_mac) { @@ -223,12 +223,11 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) * buffer. Note that 'payload_len' here is the packet_length * field which includes the padding but not the MAC. */ - if(_libssh2_timingsafe_bcmp(macbuf, - p->payload + p->total_num - mac_len, - mac_len)) { - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Failed MAC check")); - session->fullpacket_macstate = LIBSSH2_MAC_INVALID; + if(ssh2_timingsafe_bcmp(macbuf, + p->payload + p->total_num - mac_len, + mac_len)) { + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, "Failed MAC check")); + session->fullpacket_macstate = SSH2_MAC_INVALID; } else if(etm) { /* MAC was ok and we start by decrypting the first block that @@ -250,7 +249,7 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) /* we need buffer for decrypt */ decrypt_size = p->total_num - mac_len - 4; - decrypt_buffer = LIBSSH2_ALLOC(session, decrypt_size); + decrypt_buffer = SSH2_ALLOC(session, decrypt_size); if(!decrypt_buffer) { return LIBSSH2_ERROR_ALLOC; } @@ -260,7 +259,7 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) p->padding_length = first_block[0]; if(p->padding_length > p->packet_length - 1) { - LIBSSH2_FREE(session, decrypt_buffer); + SSH2_FREE(session, decrypt_buffer); return LIBSSH2_ERROR_PROTO; } @@ -274,13 +273,13 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) decrypt_buffer + blocksize - 1, decrypt_size - blocksize, LAST_BLOCK); if(rc) { - LIBSSH2_FREE(session, decrypt_buffer); + SSH2_FREE(session, decrypt_buffer); return rc; } } /* replace encrypted payload with plain text payload */ - LIBSSH2_FREE(session, p->payload); + SSH2_FREE(session, p->payload); p->payload = decrypt_buffer; } } @@ -297,7 +296,7 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) /* Check for and deal with decompression */ compressed = session->local.comp && session->local.comp->compress && - ((session->state & LIBSSH2_STATE_AUTHENTICATED) || + ((session->state & SSH2_STATE_AUTHENTICATED) || session->local.comp->use_in_auth); if(compressed && session->remote.comp_abstract) { @@ -315,7 +314,7 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) p->payload, session->fullpacket_payload_len, &session->remote.comp_abstract); - LIBSSH2_FREE(session, p->payload); + SSH2_FREE(session, p->payload); p->payload = NULL; if(rc) return rc; @@ -326,25 +325,25 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) session->fullpacket_packet_type = p->payload[0]; - debugdump(session, "libssh2_transport_read() plain", + debugdump(session, "ssh2_transport_read() plain", p->payload, session->fullpacket_payload_len); - session->fullpacket_state = libssh2_NB_state_created; + session->fullpacket_state = ssh2_NB_state_created; } - if(session->fullpacket_state == libssh2_NB_state_created) { - rc = _libssh2_packet_add(session, p->payload, - session->fullpacket_payload_len, - session->fullpacket_macstate, seq); + if(session->fullpacket_state == ssh2_NB_state_created) { + rc = ssh2_packet_add(session, p->payload, + session->fullpacket_payload_len, + session->fullpacket_macstate, seq); if(rc == LIBSSH2_ERROR_EAGAIN) return rc; if(rc) { - session->fullpacket_state = libssh2_NB_state_idle; + session->fullpacket_state = ssh2_NB_state_idle; return rc; } } - session->fullpacket_state = libssh2_NB_state_idle; + session->fullpacket_state = ssh2_NB_state_idle; if(session->kex_strict && session->fullpacket_packet_type == SSH_MSG_NEWKEYS) { @@ -363,9 +362,9 @@ static int fullpacket(LIBSSH2_SESSION *session, int encrypted /* 1 or 0 */) * This function reads the binary stream as specified in chapter 6 of RFC4253 * "The Secure Shell (SSH) Transport Layer Protocol" * - * DOES NOT call _libssh2_error() for ANY error case. + * DOES NOT call ssh2_err() for ANY error case. */ -int _libssh2_transport_read(LIBSSH2_SESSION *session) +int ssh2_transport_read(LIBSSH2_SESSION *session) { int rc; struct transportpacket *p = &session->packet; @@ -393,24 +392,24 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) /* * All channels, systems, subsystems, etc eventually make it down here * when looking for more incoming data. If a key exchange is going on - * (LIBSSH2_STATE_EXCHANGING_KEYS bit is set) then the remote end + * (SSH2_STATE_EXCHANGING_KEYS bit is set) then the remote end * ONLY sends key exchange related traffic. In non-blocking mode, there is * a chance to break out of the kex_exchange function with an EAGAIN - * status, and never come back to it. If LIBSSH2_STATE_EXCHANGING_KEYS is + * status, and never come back to it. If SSH2_STATE_EXCHANGING_KEYS is * active, then we must redirect to the key exchange. However, if * kex_exchange is active (as in it is the one that calls this execution * of packet_read, then do not redirect, as that would be an infinite loop! */ - if(session->state & LIBSSH2_STATE_EXCHANGING_KEYS && - !(session->state & LIBSSH2_STATE_KEX_ACTIVE)) { + if(session->state & SSH2_STATE_EXCHANGING_KEYS && + !(session->state & SSH2_STATE_KEX_ACTIVE)) { /* Whoever wants a packet does not get anything until the key * re-exchange is done! */ - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, "Redirecting into the" - " key re-exchange from _libssh2_transport_read")); - rc = _libssh2_kex_exchange(session, 1, &session->startup_key_state); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Redirecting into the" + " key re-exchange from ssh2_transport_read()")); + rc = ssh2_kex_exchange(session, 1, &session->startup_key_state); if(rc) return rc; } @@ -420,19 +419,19 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) * I know this is ugly and not a really good use of "goto", but * this case statement would be even uglier to do it any other way */ - if(session->readPack_state == libssh2_NB_state_jump1) { - session->readPack_state = libssh2_NB_state_idle; + if(session->readPack_state == ssh2_NB_state_jump1) { + session->readPack_state = ssh2_NB_state_idle; encrypted = session->readPack_encrypted; - goto libssh2_transport_read_point1; + goto ssh2_transport_read_point1; } do { int etm; - if(session->socket_state == LIBSSH2_SOCKET_DISCONNECTED) { + if(session->socket_state == SSH2_SOCKET_DISCONNECTED) { return LIBSSH2_ERROR_SOCKET_DISCONNECT; } - if(session->state & LIBSSH2_STATE_NEWKEYS) { + if(session->state & SSH2_STATE_NEWKEYS) { blocksize = session->remote.crypt->blocksize; } else { @@ -485,9 +484,9 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) } /* now read a big chunk from the network into the temp buffer */ - nread = LIBSSH2_RECV(session, &p->buf[remainbuf], - PACKETBUFSIZE - remainbuf, - LIBSSH2_SOCKET_RECV_FLAGS(session)); + nread = SSH2_RECV(session, &p->buf[remainbuf], + PACKETBUFSIZE - remainbuf, + SSH2_SOCKET_RECV_FLAGS(session)); if(nread <= 0) { /* check if this is due to EAGAIN and return the special return code if so, error out normally otherwise */ @@ -496,18 +495,17 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) LIBSSH2_SESSION_BLOCK_INBOUND; return LIBSSH2_ERROR_EAGAIN; } - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Error recving %ld bytes (got %ld)", - (long)(PACKETBUFSIZE - remainbuf), - (long)-nread)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Error recving %ld bytes (got %ld)", + (long)(PACKETBUFSIZE - remainbuf), (long)-nread)); return LIBSSH2_ERROR_SOCKET_RECV; } - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Recved %ld/%ld bytes to %p+%ld", (long)nread, - (long)(PACKETBUFSIZE - remainbuf), (void *)p->buf, - (long)remainbuf)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Recved %ld/%ld bytes to %p+%ld", (long)nread, + (long)(PACKETBUFSIZE - remainbuf), (void *)p->buf, + (long)remainbuf)); - debugdump(session, "libssh2_transport_read() raw", + debugdump(session, "ssh2_transport_read() raw", &p->buf[remainbuf], nread); /* advance write pointer */ p->writeidx += nread; @@ -561,17 +559,17 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) if(rc != LIBSSH2_ERROR_NONE) { p->total_num = 0; /* no packet buffer available */ if(p->payload) - LIBSSH2_FREE(session, p->payload); + SSH2_FREE(session, p->payload); p->payload = NULL; return rc; } /* store size in buffers for use below */ ptr = &block[0]; - _libssh2_store_u32(&ptr, len); + ssh2_store_u32(&ptr, len); ptr = &p->init[0]; - _libssh2_store_u32(&ptr, len); + ssh2_store_u32(&ptr, len); } else { if(encrypted) { @@ -598,7 +596,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) /* we now have the initial blocksize bytes decrypted, * and we can extract packet and padding length from it */ - p->packet_length = _libssh2_ntohu32(block); + p->packet_length = ssh2_ntohu32(block); } if(!encrypted || !CRYPT_FLAG_R(session, REQUIRES_FULL_PACKET)) { @@ -616,7 +614,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) /* we collect entire undecrypted packet including the packet length field that we run MAC over */ - p->packet_length = _libssh2_ntohu32(block); + p->packet_length = ssh2_ntohu32(block); total_num = 4 + p->packet_length + remote_mac->mac_len; } else { @@ -645,7 +643,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) */ total_num = 4; - p->packet_length = _libssh2_ntohu32(block); + p->packet_length = ssh2_ntohu32(block); if(p->packet_length < 1) return LIBSSH2_ERROR_DECRYPT; @@ -675,7 +673,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) /* Get a packet handle put data into. We get one to hold all data, including padding and MAC. */ - p->payload = LIBSSH2_ALLOC(session, total_num); + p->payload = SSH2_ALLOC(session, total_num); if(!p->payload) { return LIBSSH2_ERROR_ALLOC; } @@ -698,7 +696,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) } else { if(p->payload) - LIBSSH2_FREE(session, p->payload); + SSH2_FREE(session, p->payload); p->payload = NULL; return LIBSSH2_ERROR_OUT_OF_BOUNDARY; } @@ -763,7 +761,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) amount to decrypt even more */ if((p->data_num + numbytes) >= (p->total_num - skip)) { /* decrypt the entire rest of the package */ - numdecrypt = LIBSSH2_MAX(0, + numdecrypt = SSH2_MAX(0, (int)(p->total_num - skip) - (int)p->data_num); firstlast = LAST_BLOCK; } @@ -782,7 +780,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) if(CRYPT_FLAG_R(session, INTEGRATED_MAC)) { /* Make sure that we save enough bytes to make the last * block large enough to hold the entire integrated MAC */ - numdecrypt = LIBSSH2_MIN(numdecrypt, + numdecrypt = SSH2_MIN(numdecrypt, (int)(p->total_num - skip - blocksize - p->data_num)); numbytes = 0; } @@ -858,7 +856,7 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) } else { if(p->payload) - LIBSSH2_FREE(session, p->payload); + SSH2_FREE(session, p->payload); p->payload = NULL; return LIBSSH2_ERROR_OUT_OF_BOUNDARY; } @@ -877,13 +875,13 @@ int _libssh2_transport_read(LIBSSH2_SESSION *session) if(!remainpack) { /* we have a full packet */ -libssh2_transport_read_point1: +ssh2_transport_read_point1: rc = fullpacket(session, encrypted); if(rc == LIBSSH2_ERROR_EAGAIN) { - if(session->packAdd_state != libssh2_NB_state_idle) { + if(session->packAdd_state != ssh2_NB_state_idle) { /* fullpacket only returns LIBSSH2_ERROR_EAGAIN if - * libssh2_packet_add() returns LIBSSH2_ERROR_EAGAIN. If + * ssh2_packet_add() returns LIBSSH2_ERROR_EAGAIN. If * that returns LIBSSH2_ERROR_EAGAIN but the packAdd_state * is idle, then the packet has been added to the brigade, * but some immediate action that was taken based on the @@ -892,7 +890,7 @@ libssh2_transport_read_point1: * in. */ session->readPack_encrypted = encrypted; - session->readPack_state = libssh2_NB_state_jump1; + session->readPack_state = ssh2_NB_state_jump1; } return rc; @@ -926,8 +924,8 @@ static int send_existing(LIBSSH2_SESSION *session, const unsigned char *data, we do not add this one up until the previous one has been sent. To make the caller really notice his/hers flaw, we return error for this case */ - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Address is different, returning EAGAIN")); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Address is different, returning EAGAIN")); return LIBSSH2_ERROR_EAGAIN; } @@ -936,17 +934,16 @@ static int send_existing(LIBSSH2_SESSION *session, const unsigned char *data, /* number of bytes left to send */ length = p->ototal_num - p->osent; - rc = LIBSSH2_SEND(session, &p->outbuf[p->osent], length, - LIBSSH2_SOCKET_SEND_FLAGS(session)); + rc = SSH2_SEND(session, &p->outbuf[p->osent], length, + SSH2_SOCKET_SEND_FLAGS(session)); if(rc < 0) - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Error sending %ld bytes: %ld", - (long)length, (long)-rc)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Error sending %ld bytes: %ld", (long)length, (long)-rc)); else { - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Sent %ld/%ld bytes at %p+%lu", (long)rc, (long)length, - (void *)p->outbuf, (unsigned long)p->osent)); - debugdump(session, "libssh2_transport_write send()", + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Sent %ld/%ld bytes at %p+%lu", (long)rc, (long)length, + (void *)p->outbuf, (unsigned long)p->osent)); + debugdump(session, "ssh2_transport_send()", &p->outbuf[p->osent], rc); } @@ -988,14 +985,14 @@ static int send_existing(LIBSSH2_SESSION *session, const unsigned char *data, * then be called with the same argument set (same data pointer and same * data_len) until ERROR_NONE or failure is returned. * - * This function DOES NOT call _libssh2_error() on any errors. + * This function DOES NOT call ssh2_err() on any errors. */ -int _libssh2_transport_send(LIBSSH2_SESSION *session, - const unsigned char *data, size_t data_len, - const unsigned char *data2, size_t data2_len) +int ssh2_transport_send(LIBSSH2_SESSION *session, + const unsigned char *data, size_t data_len, + const unsigned char *data2, size_t data2_len) { int blocksize = - (session->state & LIBSSH2_STATE_NEWKEYS) ? + (session->state & SSH2_STATE_NEWKEYS) ? session->local.crypt->blocksize : 8; ssize_t padding_length; size_t packet_length; @@ -1021,22 +1018,22 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, * exchange, we must complete that key exchange before continuing to write * further data. * - * See the similar block in _libssh2_transport_read for more details. + * See the similar block in ssh2_transport_read() for more details. */ - if(session->state & LIBSSH2_STATE_EXCHANGING_KEYS && - !(session->state & LIBSSH2_STATE_KEX_ACTIVE)) { + if(session->state & SSH2_STATE_EXCHANGING_KEYS && + !(session->state & SSH2_STATE_KEX_ACTIVE)) { /* Do not write any new packets if we are still in the middle of a key * exchange. */ - _libssh2_debug((session, LIBSSH2_TRACE_TRANS, "Redirecting into the" - " key re-exchange from _libssh2_transport_send")); - rc = _libssh2_kex_exchange(session, 1, &session->startup_key_state); + ssh2_deb((session, LIBSSH2_TRACE_TRANS, "Redirecting into the" + " key re-exchange from ssh2_transport_send()")); + rc = ssh2_kex_exchange(session, 1, &session->startup_key_state); if(rc) return rc; } - debugdump(session, "libssh2_transport_write plain", data, data_len); + debugdump(session, "ssh2_transport_send() plain", data, data_len); if(data2) - debugdump(session, "libssh2_transport_write plain2", data2, data2_len); + debugdump(session, "ssh2_transport_send() plain2", data2, data2_len); /* FIRST, check if we have a pending write to complete. send_existing only sanity-check data and data_len and not data2 and data2_len! */ @@ -1050,7 +1047,7 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, /* set by send_existing if data was sent */ return rc; - encrypted = (session->state & LIBSSH2_STATE_NEWKEYS) ? 1 : 0; + encrypted = (session->state & SSH2_STATE_NEWKEYS) ? 1 : 0; if(encrypted && session->local.crypt && CRYPT_FLAG_L(session, REQUIRES_FULL_PACKET)) { @@ -1064,7 +1061,7 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, compressed = session->local.comp && session->local.comp->compress && - ((session->state & LIBSSH2_STATE_AUTHENTICATED) || + ((session->state & SSH2_STATE_AUTHENTICATED) || session->local.comp->use_in_auth); if(encrypted && compressed && session->local.comp_abstract) { @@ -1162,14 +1159,14 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, /* store packet_length, which is the size of the whole packet except the MAC and the packet_length field itself */ - _libssh2_htonu32(p->outbuf, (uint32_t)(packet_length - 4)); + ssh2_htonu32(p->outbuf, (uint32_t)(packet_length - 4)); /* store padding_length */ p->outbuf[4] = (unsigned char)padding_length; /* fill the padding area with random junk */ - if(_libssh2_random(p->outbuf + 5 + data_len, padding_length)) { - return _libssh2_error(session, LIBSSH2_ERROR_RANDGEN, - "Unable to get random bytes for packet padding"); + if(ssh2_random(p->outbuf + 5 + data_len, padding_length)) { + return ssh2_err(session, LIBSSH2_ERROR_RANDGEN, + "Unable to get random bytes for packet padding"); } if(encrypted) { @@ -1186,8 +1183,8 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, session->local.seqno, p->outbuf, packet_length, NULL, 0, &session->local.mac_abstract)) - return _libssh2_error(session, LIBSSH2_ERROR_MAC_FAILURE, - "Failed to calculate MAC"); + return ssh2_err(session, LIBSSH2_ERROR_MAC_FAILURE, + "Failed to calculate MAC"); } if(CRYPT_FLAG_L(session, REQUIRES_FULL_PACKET)) { @@ -1209,8 +1206,8 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, for(i = etm_crypt_offset; i < packet_length; i += session->local.crypt->blocksize) { unsigned char *ptr = &p->outbuf[i]; - size_t bsize = LIBSSH2_MIN(session->local.crypt->blocksize, - (int)(packet_length - i)); + size_t bsize = SSH2_MIN(session->local.crypt->blocksize, + (int)(packet_length - i)); /* The INTEGRATED_MAC case always has an extra call below, so it never is LAST_BLOCK up here. */ int firstlast = i == 0 ? FIRST_BLOCK : @@ -1230,11 +1227,10 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, /* advance the loop counter by the extra amount */ i += bsize - session->local.crypt->blocksize; } - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "crypting bytes %lu-%lu", (unsigned long)i, - (unsigned long)(i + bsize - 1))); - if(session->local.crypt->crypt(session, 0, ptr, - bsize, + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "crypting bytes %lu-%lu", (unsigned long)i, + (unsigned long)(i + bsize - 1))); + if(session->local.crypt->crypt(session, 0, ptr, bsize, &session->local.crypt_abstract, firstlast)) return LIBSSH2_ERROR_ENCRYPT; /* encryption failure */ @@ -1246,8 +1242,7 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, int authlen = local_mac ? local_mac->mac_len : 0; assert((size_t)total_length <= packet_length + session->local.crypt->blocksize); - if(session->local.crypt->crypt(session, - 0, + if(session->local.crypt->crypt(session, 0, &p->outbuf[packet_length], authlen, &session->local.crypt_abstract, @@ -1266,8 +1261,8 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, session->local.seqno, p->outbuf, packet_length, NULL, 0, &session->local.mac_abstract)) - return _libssh2_error(session, LIBSSH2_ERROR_MAC_FAILURE, - "Failed to calculate MAC"); + return ssh2_err(session, LIBSSH2_ERROR_MAC_FAILURE, + "Failed to calculate MAC"); } } @@ -1277,17 +1272,17 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, session->local.seqno = 0; } - ret = LIBSSH2_SEND(session, p->outbuf, total_length, - LIBSSH2_SOCKET_SEND_FLAGS(session)); + ret = SSH2_SEND(session, p->outbuf, total_length, + SSH2_SOCKET_SEND_FLAGS(session)); if(ret < 0) - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Error sending %ld bytes: %ld", - (long)total_length, (long)-ret)); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Error sending %ld bytes: %ld", + (long)total_length, (long)-ret)); else { - _libssh2_debug((session, LIBSSH2_TRACE_SOCKET, - "Sent %ld/%ld bytes at %p", - (long)ret, (long)total_length, (void *)p->outbuf)); - debugdump(session, "libssh2_transport_write send()", p->outbuf, ret); + ssh2_deb((session, LIBSSH2_TRACE_SOCKET, + "Sent %ld/%ld bytes at %p", + (long)ret, (long)total_length, (void *)p->outbuf)); + debugdump(session, "ssh2_transport_send()", p->outbuf, ret); } if(ret != total_length) { diff --git a/src/transport.h b/src/transport.h index 68d50897..2890fe42 100644 --- a/src/transport.h +++ b/src/transport.h @@ -46,7 +46,7 @@ #include "packet.h" /* - * libssh2_transport_send + * ssh2_transport_send() * * Send a packet, encrypting it and adding a MAC code if necessary * Returns 0 on success, non-zero on failure. @@ -61,14 +61,14 @@ * then be called with the same argument set (same data pointer and same * data_len) until ERROR_NONE or failure is returned. * - * This function DOES NOT call _libssh2_error() on any errors. + * This function DOES NOT call ssh2_err() on any errors. */ -int _libssh2_transport_send(LIBSSH2_SESSION *session, - const unsigned char *data, size_t data_len, - const unsigned char *data2, size_t data2_len); +int ssh2_transport_send(LIBSSH2_SESSION *session, + const unsigned char *data, size_t data_len, + const unsigned char *data2, size_t data2_len); /* - * _libssh2_transport_read + * ssh2_transport_read() * * Collect a packet into the input brigade block only controls whether or not * to wait for a packet to start. @@ -82,6 +82,6 @@ int _libssh2_transport_send(LIBSSH2_SESSION *session, * This function reads the binary stream as specified in chapter 6 of RFC4253 * "The Secure Shell (SSH) Transport Layer Protocol" */ -int _libssh2_transport_read(LIBSSH2_SESSION *session); +int ssh2_transport_read(LIBSSH2_SESSION *session); #endif /* LIBSSH2_TRANSPORT_H */ diff --git a/src/userauth.c b/src/userauth.c index 039a40f8..0fad9039 100644 --- a/src/userauth.c +++ b/src/userauth.c @@ -86,156 +86,155 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username, unsigned char *s; int rc; - if(session->userauth_list_state == libssh2_NB_state_idle) { + if(session->userauth_list_state == ssh2_NB_state_idle) { /* Zero the whole thing out */ memset(&session->userauth_list_packet_requirev_state, 0, sizeof(session->userauth_list_packet_requirev_state)); if(username_len > UINT32_MAX - 27) { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "username_len out of bounds"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "username_len out of bounds"); return NULL; } session->userauth_list_data_len = username_len + 27; if(session->userauth_list_data) { - LIBSSH2_FREE(session, session->userauth_list_data); + SSH2_FREE(session, session->userauth_list_data); } s = session->userauth_list_data = - LIBSSH2_ALLOC(session, session->userauth_list_data_len); + SSH2_ALLOC(session, session->userauth_list_data_len); if(!session->userauth_list_data) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for userauth_list"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for userauth_list"); return NULL; } *(s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_store_str(&s, username, username_len); - _libssh2_store_str(&s, "ssh-connection", 14); - _libssh2_store_u32(&s, 4); /* send "none" separately */ + ssh2_store_str(&s, username, username_len); + ssh2_store_str(&s, "ssh-connection", 14); + ssh2_store_u32(&s, 4); /* send "none" separately */ - session->userauth_list_state = libssh2_NB_state_created; + session->userauth_list_state = ssh2_NB_state_created; } - if(session->userauth_list_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, session->userauth_list_data, - session->userauth_list_data_len, - (const unsigned char *)"none", 4); + if(session->userauth_list_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, session->userauth_list_data, + session->userauth_list_data_len, + (const unsigned char *)"none", 4); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block requesting userauth list"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block requesting userauth list"); return NULL; } /* now free the packet that was sent */ - LIBSSH2_FREE(session, session->userauth_list_data); + SSH2_FREE(session, session->userauth_list_data); session->userauth_list_data = NULL; if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send userauth-none request"); - session->userauth_list_state = libssh2_NB_state_idle; + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send userauth-none request"); + session->userauth_list_state = ssh2_NB_state_idle; return NULL; } - session->userauth_list_state = libssh2_NB_state_sent; + session->userauth_list_state = ssh2_NB_state_sent; } - if(session->userauth_list_state == libssh2_NB_state_sent) { - rc = _libssh2_packet_requirev(session, reply_codes, - &session->userauth_list_data, - &session->userauth_list_data_len, 0, - NULL, 0, - &session->userauth_list_packet_requirev_state); + if(session->userauth_list_state == ssh2_NB_state_sent) { + rc = ssh2_packet_requirev(session, reply_codes, + &session->userauth_list_data, + &session->userauth_list_data_len, 0, + NULL, 0, + &session->userauth_list_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block requesting userauth list"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block requesting userauth list"); return NULL; } else if(rc || (session->userauth_list_data_len < 1)) { - _libssh2_error(session, rc, "Failed getting response"); - session->userauth_list_state = libssh2_NB_state_idle; + ssh2_err(session, rc, "Failed getting response"); + session->userauth_list_state = ssh2_NB_state_idle; return NULL; } if(session->userauth_list_data[0] == SSH_MSG_USERAUTH_BANNER) { if(session->userauth_list_data_len < 5) { - LIBSSH2_FREE(session, session->userauth_list_data); + SSH2_FREE(session, session->userauth_list_data); session->userauth_list_data = NULL; - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet size"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected packet size"); return NULL; } - banner_len = _libssh2_ntohu32(session->userauth_list_data + 1); + banner_len = ssh2_ntohu32(session->userauth_list_data + 1); if(banner_len > session->userauth_list_data_len - 5) { - LIBSSH2_FREE(session, session->userauth_list_data); + SSH2_FREE(session, session->userauth_list_data); session->userauth_list_data = NULL; - _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Unexpected userauth banner size"); + ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Unexpected userauth banner size"); return NULL; } if(session->userauth_banner) { - LIBSSH2_FREE(session, session->userauth_banner); + SSH2_FREE(session, session->userauth_banner); } - session->userauth_banner = LIBSSH2_ALLOC(session, banner_len + 1); + session->userauth_banner = SSH2_ALLOC(session, banner_len + 1); if(!session->userauth_banner) { - LIBSSH2_FREE(session, session->userauth_list_data); + SSH2_FREE(session, session->userauth_list_data); session->userauth_list_data = NULL; - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for userauth banner"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for userauth banner"); return NULL; } memcpy(session->userauth_banner, session->userauth_list_data + 5, banner_len); session->userauth_banner[banner_len] = '\0'; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Banner: %s", session->userauth_banner)); - LIBSSH2_FREE(session, session->userauth_list_data); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, "Banner: %s", + session->userauth_banner)); + SSH2_FREE(session, session->userauth_list_data); session->userauth_list_data = NULL; /* SSH_MSG_USERAUTH_BANNER has been handled */ reply_codes[2] = 0; - rc = _libssh2_packet_requirev(session, reply_codes, - &session->userauth_list_data, - &session->userauth_list_data_len, 0, - NULL, 0, + rc = ssh2_packet_requirev(session, reply_codes, + &session->userauth_list_data, + &session->userauth_list_data_len, 0, + NULL, 0, &session->userauth_list_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block requesting userauth list"); + ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block requesting userauth list"); return NULL; } else if(rc || (session->userauth_list_data_len < 1)) { - _libssh2_error(session, rc, "Failed getting response"); - session->userauth_list_state = libssh2_NB_state_idle; + ssh2_err(session, rc, "Failed getting response"); + session->userauth_list_state = ssh2_NB_state_idle; return NULL; } } if(session->userauth_list_data[0] == SSH_MSG_USERAUTH_SUCCESS) { /* Wow, who'dve thought... */ - _libssh2_error(session, LIBSSH2_ERROR_NONE, "No error"); - LIBSSH2_FREE(session, session->userauth_list_data); + ssh2_err(session, LIBSSH2_ERROR_NONE, "No error"); + SSH2_FREE(session, session->userauth_list_data); session->userauth_list_data = NULL; - session->state |= LIBSSH2_STATE_AUTHENTICATED; - session->userauth_list_state = libssh2_NB_state_idle; + session->state |= SSH2_STATE_AUTHENTICATED; + session->userauth_list_state = ssh2_NB_state_idle; return NULL; } if(session->userauth_list_data_len < 5) { - LIBSSH2_FREE(session, session->userauth_list_data); + SSH2_FREE(session, session->userauth_list_data); session->userauth_list_data = NULL; - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet size"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, "Unexpected packet size"); return NULL; } - methods_len = _libssh2_ntohu32(session->userauth_list_data + 1); + methods_len = ssh2_ntohu32(session->userauth_list_data + 1); if(methods_len >= session->userauth_list_data_len - 5) { - _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Unexpected userauth list size"); + ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Unexpected userauth list size"); return NULL; } @@ -243,12 +242,11 @@ static char *userauth_list(LIBSSH2_SESSION *session, const char *username, memmove(session->userauth_list_data, session->userauth_list_data + 5, methods_len); session->userauth_list_data[methods_len] = '\0'; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Permitted auth methods: %s", - session->userauth_list_data)); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, "Permitted auth methods: %s", + session->userauth_list_data)); } - session->userauth_list_state = libssh2_NB_state_idle; + session->userauth_list_state = ssh2_NB_state_idle; return (char *)session->userauth_list_data; } @@ -280,9 +278,8 @@ int libssh2_userauth_banner(LIBSSH2_SESSION *session, char **banner) return LIBSSH2_ERROR_MISSING_USERAUTH_BANNER; if(!session->userauth_banner) { - return _libssh2_error(session, - LIBSSH2_ERROR_MISSING_USERAUTH_BANNER, - "Missing userauth banner"); + return ssh2_err(session, LIBSSH2_ERROR_MISSING_USERAUTH_BANNER, + "Missing userauth banner"); } if(banner) @@ -298,18 +295,18 @@ int libssh2_userauth_banner(LIBSSH2_SESSION *session, char **banner) LIBSSH2_API int libssh2_userauth_authenticated(LIBSSH2_SESSION *session) { - return (session->state & LIBSSH2_STATE_AUTHENTICATED) ? 1 : 0; + return (session->state & SSH2_STATE_AUTHENTICATED) ? 1 : 0; } /* * Plain ol' login */ static int userauth_password(LIBSSH2_SESSION *session, - const char *username, - unsigned int username_len, - const unsigned char *password, - unsigned int password_len, - LIBSSH2_PASSWD_CHANGEREQ_FUNC(*passwd_change_cb)) + const char *username, + unsigned int username_len, + const unsigned char *password, + unsigned int password_len, + LIBSSH2_PASSWD_CHANGEREQ_FUNC(*passwd_change_cb)) { unsigned char *s; static const unsigned char reply_codes[4] = { @@ -320,7 +317,7 @@ static int userauth_password(LIBSSH2_SESSION *session, }; int rc; - if(session->userauth_pswd_state == libssh2_NB_state_idle) { + if(session->userauth_pswd_state == ssh2_NB_state_idle) { /* Zero the whole thing out */ memset(&session->userauth_pswd_packet_requirev_state, 0, sizeof(session->userauth_pswd_packet_requirev_state)); @@ -330,8 +327,8 @@ static int userauth_password(LIBSSH2_SESSION *session, * service(14)"ssh-connection" + method_len(4) + method(8)"password" + * chgpwdbool(1) + password_len(4) */ if(username_len > UINT32_MAX - 40) { - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "username_len out of bounds"); + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "username_len out of bounds"); } session->userauth_pswd_data_len = username_len + 40; @@ -342,107 +339,104 @@ static int userauth_password(LIBSSH2_SESSION *session, /* TODO: remove this alloc with a fixed buffer in the session struct */ s = session->userauth_pswd_data = - LIBSSH2_ALLOC(session, session->userauth_pswd_data_len); + SSH2_ALLOC(session, session->userauth_pswd_data_len); if(!session->userauth_pswd_data) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "userauth-password request"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "userauth-password request"); } *(s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_store_str(&s, username, username_len); - _libssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1); - _libssh2_store_str(&s, "password", sizeof("password") - 1); + ssh2_store_str(&s, username, username_len); + ssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1); + ssh2_store_str(&s, "password", sizeof("password") - 1); *s++ = '\0'; - _libssh2_store_u32(&s, password_len); + ssh2_store_u32(&s, password_len); /* 'password' is sent separately */ - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Attempting to login using password authentication")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Attempting to login using password authentication")); - session->userauth_pswd_state = libssh2_NB_state_created; + session->userauth_pswd_state = ssh2_NB_state_created; } - if(session->userauth_pswd_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, session->userauth_pswd_data, - session->userauth_pswd_data_len, - password, password_len); + if(session->userauth_pswd_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, session->userauth_pswd_data, + session->userauth_pswd_data_len, + password, password_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block writing password request"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block writing password request"); } /* now free the sent packet */ - LIBSSH2_FREE(session, session->userauth_pswd_data); + SSH2_FREE(session, session->userauth_pswd_data); session->userauth_pswd_data = NULL; if(rc) { - session->userauth_pswd_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send userauth-password request"); + session->userauth_pswd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send userauth-password request"); } - session->userauth_pswd_state = libssh2_NB_state_sent; + session->userauth_pswd_state = ssh2_NB_state_sent; } password_response: - if((session->userauth_pswd_state == libssh2_NB_state_sent) || - (session->userauth_pswd_state == libssh2_NB_state_sent1) || - (session->userauth_pswd_state == libssh2_NB_state_sent2)) { - if(session->userauth_pswd_state == libssh2_NB_state_sent) { - rc = _libssh2_packet_requirev(session, reply_codes, - &session->userauth_pswd_data, - &session->userauth_pswd_data_len, - 0, NULL, 0, - &session-> - userauth_pswd_packet_requirev_state); + if((session->userauth_pswd_state == ssh2_NB_state_sent) || + (session->userauth_pswd_state == ssh2_NB_state_sent1) || + (session->userauth_pswd_state == ssh2_NB_state_sent2)) { + if(session->userauth_pswd_state == ssh2_NB_state_sent) { + rc = ssh2_packet_requirev(session, reply_codes, + &session->userauth_pswd_data, + &session->userauth_pswd_data_len, + 0, NULL, 0, + &session-> + userauth_pswd_packet_requirev_state); if(rc) { if(rc != LIBSSH2_ERROR_EAGAIN) - session->userauth_pswd_state = libssh2_NB_state_idle; + session->userauth_pswd_state = ssh2_NB_state_idle; - return _libssh2_error(session, rc, - "Waiting for password response"); + return ssh2_err(session, rc, "Waiting for password response"); } else if(session->userauth_pswd_data_len < 1) { - session->userauth_pswd_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet size"); + session->userauth_pswd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected packet size"); } if(session->userauth_pswd_data[0] == SSH_MSG_USERAUTH_SUCCESS) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Password authentication successful")); - LIBSSH2_FREE(session, session->userauth_pswd_data); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Password authentication successful")); + SSH2_FREE(session, session->userauth_pswd_data); session->userauth_pswd_data = NULL; - session->state |= LIBSSH2_STATE_AUTHENTICATED; - session->userauth_pswd_state = libssh2_NB_state_idle; + session->state |= SSH2_STATE_AUTHENTICATED; + session->userauth_pswd_state = ssh2_NB_state_idle; return 0; } else if(session->userauth_pswd_data[0] == SSH_MSG_USERAUTH_FAILURE) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Password authentication failed")); - LIBSSH2_FREE(session, session->userauth_pswd_data); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Password authentication failed")); + SSH2_FREE(session, session->userauth_pswd_data); session->userauth_pswd_data = NULL; - session->userauth_pswd_state = libssh2_NB_state_idle; - return _libssh2_error(session, - LIBSSH2_ERROR_AUTHENTICATION_FAILED, - "Authentication failed " - "(username/password)"); + session->userauth_pswd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, + "Authentication failed (username/password)"); } session->userauth_pswd_newpw = NULL; session->userauth_pswd_newpw_len = 0; - session->userauth_pswd_state = libssh2_NB_state_sent1; + session->userauth_pswd_state = ssh2_NB_state_sent1; } if(session->userauth_pswd_data_len < 1) { - session->userauth_pswd_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unexpected packet size"); + session->userauth_pswd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unexpected packet size"); } if((session->userauth_pswd_data[0] == @@ -451,26 +445,25 @@ password_response: SSH_MSG_USERAUTH_PASSWD_CHANGEREQ)) { session->userauth_pswd_data0 = SSH_MSG_USERAUTH_PASSWD_CHANGEREQ; - if((session->userauth_pswd_state == libssh2_NB_state_sent1) || - (session->userauth_pswd_state == libssh2_NB_state_sent2)) { - if(session->userauth_pswd_state == libssh2_NB_state_sent1) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Password change required")); - LIBSSH2_FREE(session, session->userauth_pswd_data); + if((session->userauth_pswd_state == ssh2_NB_state_sent1) || + (session->userauth_pswd_state == ssh2_NB_state_sent2)) { + if(session->userauth_pswd_state == ssh2_NB_state_sent1) { + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Password change required")); + SSH2_FREE(session, session->userauth_pswd_data); session->userauth_pswd_data = NULL; } if(passwd_change_cb) { - if(session->userauth_pswd_state == - libssh2_NB_state_sent1) { + if(session->userauth_pswd_state == ssh2_NB_state_sent1) { passwd_change_cb(session, &session->userauth_pswd_newpw, &session->userauth_pswd_newpw_len, &session->abstract); if(!session->userauth_pswd_newpw) { - return _libssh2_error(session, - LIBSSH2_ERROR_PASSWORD_EXPIRED, - "Password expired, and " - "callback failed"); + return ssh2_err(session, + LIBSSH2_ERROR_PASSWORD_EXPIRED, + "Password expired, and " + "callback failed"); } /* basic data_len + newpw_len(4) */ @@ -478,8 +471,8 @@ password_response: session->userauth_pswd_data_len = username_len + password_len + 44; s = session->userauth_pswd_data = - LIBSSH2_ALLOC(session, - session->userauth_pswd_data_len); + SSH2_ALLOC(session, + session->userauth_pswd_data_len); } else { s = session->userauth_pswd_data = NULL; @@ -487,83 +480,76 @@ password_response: } if(!session->userauth_pswd_data) { - LIBSSH2_FREE(session, - session->userauth_pswd_newpw); + SSH2_FREE(session, session->userauth_pswd_newpw); session->userauth_pswd_newpw = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory " - "for userauth password " - "change request"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory " + "for userauth password " + "change request"); } *(s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_store_str(&s, username, username_len); - _libssh2_store_str(&s, "ssh-connection", - sizeof("ssh-connection") - 1); - _libssh2_store_str(&s, "password", - sizeof("password") - 1); + ssh2_store_str(&s, username, username_len); + ssh2_store_str(&s, "ssh-connection", + sizeof("ssh-connection") - 1); + ssh2_store_str(&s, "password", sizeof("password") - 1); *s++ = 0x01; - _libssh2_store_str(&s, (const char *)password, - password_len); - _libssh2_store_u32(&s, - session->userauth_pswd_newpw_len); + ssh2_store_str(&s, (const char *)password, + password_len); + ssh2_store_u32(&s, session->userauth_pswd_newpw_len); /* send session->userauth_pswd_newpw separately */ - session->userauth_pswd_state = libssh2_NB_state_sent2; + session->userauth_pswd_state = ssh2_NB_state_sent2; } - if(session->userauth_pswd_state == - libssh2_NB_state_sent2) { - rc = _libssh2_transport_send(session, + if(session->userauth_pswd_state == ssh2_NB_state_sent2) { + rc = ssh2_transport_send(session, session->userauth_pswd_data, session->userauth_pswd_data_len, (const unsigned char *) session->userauth_pswd_newpw, session->userauth_pswd_newpw_len); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, - LIBSSH2_ERROR_EAGAIN, - "Would block waiting"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting"); } /* free the allocated packets again */ - LIBSSH2_FREE(session, session->userauth_pswd_data); + SSH2_FREE(session, session->userauth_pswd_data); session->userauth_pswd_data = NULL; - LIBSSH2_FREE(session, session->userauth_pswd_newpw); + SSH2_FREE(session, session->userauth_pswd_newpw); session->userauth_pswd_newpw = NULL; if(rc) { - return _libssh2_error(session, - LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send userauth " - "password-change request"); + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send userauth " + "password-change request"); } /* * Ugliest use of goto ever. Blame it on the * askN => requirev migration. */ - session->userauth_pswd_state = libssh2_NB_state_sent; + session->userauth_pswd_state = ssh2_NB_state_sent; goto password_response; } } } else { - session->userauth_pswd_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PASSWORD_EXPIRED, - "Password Expired, and no callback " - "specified"); + session->userauth_pswd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PASSWORD_EXPIRED, + "Password Expired, and no callback specified"); } } } /* FAILURE */ - LIBSSH2_FREE(session, session->userauth_pswd_data); + SSH2_FREE(session, session->userauth_pswd_data); session->userauth_pswd_data = NULL; - session->userauth_pswd_state = libssh2_NB_state_idle; + session->userauth_pswd_state = ssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, - "Authentication failed"); + return ssh2_err(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, + "Authentication failed"); } /* @@ -598,14 +584,14 @@ static int memory_read_publickey(LIBSSH2_SESSION *session, size_t tmp_len; if(pubkeyfiledata_len <= 1) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid data in public key file"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid data in public key file"); } - pubkey = LIBSSH2_ALLOC(session, pubkeyfiledata_len); + pubkey = SSH2_ALLOC(session, pubkeyfiledata_len); if(!pubkey) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for public key data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for public key data"); } memcpy(pubkey, pubkeyfiledata, pubkeyfiledata_len); @@ -617,16 +603,16 @@ static int memory_read_publickey(LIBSSH2_SESSION *session, pubkey_len--; if(!pubkey_len) { - LIBSSH2_FREE(session, pubkey); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Missing public key data"); + SSH2_FREE(session, pubkey); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Missing public key data"); } sp1 = memchr(pubkey, ' ', pubkey_len); if(!sp1) { - LIBSSH2_FREE(session, pubkey); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid public key data"); + SSH2_FREE(session, pubkey); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid public key data"); } sp1++; @@ -637,11 +623,11 @@ static int memory_read_publickey(LIBSSH2_SESSION *session, sp2 = pubkey + pubkey_len; } - if(_libssh2_base64_decode(session, (char **)&tmp, &tmp_len, - (const char *)sp1, sp2 - sp1)) { - LIBSSH2_FREE(session, pubkey); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid key data, not base64 encoded"); + if(ssh2_base64_decode(session, (char **)&tmp, &tmp_len, (const char *)sp1, + sp2 - sp1)) { + SSH2_FREE(session, pubkey); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid key data, not base64 encoded"); } /* Wasting some bytes here (okay, more than some), but since it is likely @@ -678,13 +664,13 @@ static int file_read_publickey(LIBSSH2_SESSION *session, size_t pubkey_len = 0, sp_len; size_t tmp_len; - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, "Loading public key file: %s", - pubkeyfile)); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, "Loading public key file: %s", + pubkeyfile)); /* Read Public Key */ fd = fopen(pubkeyfile, "rb"); if(!fd) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to open public key file"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to open public key file"); } while(!feof(fd) && fread(&c, 1, 1, fd) == 1 && c != '\r' && c != '\n') { pubkey_len++; @@ -693,21 +679,21 @@ static int file_read_publickey(LIBSSH2_SESSION *session, if(pubkey_len <= 1) { fclose(fd); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid data in public key file"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid data in public key file"); } - pubkey = LIBSSH2_ALLOC(session, pubkey_len); + pubkey = SSH2_ALLOC(session, pubkey_len); if(!pubkey) { fclose(fd); - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for public key data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for public key data"); } if(fread(pubkey, 1, pubkey_len, fd) != pubkey_len) { - LIBSSH2_FREE(session, pubkey); + SSH2_FREE(session, pubkey); fclose(fd); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to read public key from file"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to read public key from file"); } fclose(fd); /* @@ -718,16 +704,16 @@ static int file_read_publickey(LIBSSH2_SESSION *session, } if(!pubkey_len) { - LIBSSH2_FREE(session, pubkey); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Missing public key data"); + SSH2_FREE(session, pubkey); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Missing public key data"); } sp1 = memchr(pubkey, ' ', pubkey_len); if(!sp1) { - LIBSSH2_FREE(session, pubkey); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid public key data"); + SSH2_FREE(session, pubkey); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid public key data"); } sp1++; @@ -739,11 +725,11 @@ static int file_read_publickey(LIBSSH2_SESSION *session, sp2 = pubkey + pubkey_len; } - if(_libssh2_base64_decode(session, (char **)&tmp, &tmp_len, - (const char *)sp1, sp2 - sp1)) { - LIBSSH2_FREE(session, pubkey); - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid key data, not base64 encoded"); + if(ssh2_base64_decode(session, (char **)&tmp, &tmp_len, (const char *)sp1, + sp2 - sp1)) { + SSH2_FREE(session, pubkey); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid key data, not base64 encoded"); } /* Wasting some bytes here (okay, more than some), but since it is likely @@ -768,7 +754,7 @@ static int memory_read_privatekey(LIBSSH2_SESSION *session, const char *passphrase) { const struct hostkey_method **hostkey_methods_avail = - libssh2_hostkey_methods(); + ssh2_hostkey_methods(); *hostkey_method = NULL; *hostkey_abstract = NULL; @@ -782,16 +768,16 @@ static int memory_read_privatekey(LIBSSH2_SESSION *session, hostkey_methods_avail++; } if(!*hostkey_method) { - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE, - "No handler for specified private key"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NONE, + "No handler for specified private key"); } if((*hostkey_method)-> initPEMFromMemory(session, privkeyfiledata, privkeyfiledata_len, (const unsigned char *)passphrase, hostkey_abstract)) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to initialize private key from memory"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to initialize private key from memory"); } return 0; @@ -809,10 +795,10 @@ static int file_read_privatekey(LIBSSH2_SESSION *session, const char *passphrase) { const struct hostkey_method **hostkey_methods_avail = - libssh2_hostkey_methods(); + ssh2_hostkey_methods(); - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Loading private key file: %s", privkeyfile)); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, "Loading private key file: %s", + privkeyfile)); *hostkey_method = NULL; *hostkey_abstract = NULL; while(*hostkey_methods_avail && (*hostkey_methods_avail)->name) { @@ -825,15 +811,15 @@ static int file_read_privatekey(LIBSSH2_SESSION *session, hostkey_methods_avail++; } if(!*hostkey_method) { - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE, - "No handler for specified private key"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NONE, + "No handler for specified private key"); } if((*hostkey_method)-> initPEM(session, privkeyfile, (const unsigned char *)passphrase, hostkey_abstract)) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to initialize private key from file"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to initialize private key from file"); } return 0; @@ -873,8 +859,8 @@ static int sign_frommemory(LIBSSH2_SESSION *session, if(!privkeyobj) return -1; - libssh2_prepare_iovec(&datavec, 1); - datavec.iov_base = (void *)LIBSSH2_UNCONST(data); + ssh2_prepare_iovec(&datavec, 1); + datavec.iov_base = SSH2_UNCONST(data); datavec.iov_len = data_len; if(privkeyobj->signv(session, sig, sig_len, 1, &datavec, @@ -913,8 +899,8 @@ static int sign_fromfile(LIBSSH2_SESSION *session, if(!privkeyobj) return -1; - libssh2_prepare_iovec(&datavec, 1); - datavec.iov_base = (void *)LIBSSH2_UNCONST(data); + ssh2_prepare_iovec(&datavec, 1); + datavec.iov_base = SSH2_UNCONST(data); datavec.iov_len = data_len; if(privkeyobj->signv(session, sig, sig_len, 1, &datavec, @@ -963,55 +949,51 @@ int libssh2_sign_sk(LIBSSH2_SESSION *session, /* sig length, sig_r, sig_s, flags, counter, plus 4 bytes for each component's length, and up to 1 extra byte for each component */ *sig_len = 4 + 5 + sig_info.sig_r_len + 5 + sig_info.sig_s_len + 5; - *sig = LIBSSH2_ALLOC(session, *sig_len); + *sig = SSH2_ALLOC(session, *sig_len); if(*sig) { unsigned char *x = *sig; p = *sig; - _libssh2_store_u32(&p, 0); + ssh2_store_u32(&p, 0); - if(_libssh2_store_bignum2_bytes(&p, - sig_info.sig_r, - sig_info.sig_r_len) && - _libssh2_store_bignum2_bytes(&p, - sig_info.sig_s, - sig_info.sig_s_len)) { + if(ssh2_store_bignum_bytes(&p, sig_info.sig_r, + sig_info.sig_r_len) && + ssh2_store_bignum_bytes(&p, sig_info.sig_s, + sig_info.sig_s_len)) { *sig_len = p - *sig; - _libssh2_store_u32(&x, (uint32_t)(*sig_len - 4)); + ssh2_store_u32(&x, (uint32_t)(*sig_len - 4)); } else { - _libssh2_debug((session, LIBSSH2_ERROR_STORE_OVERFLOW, - "Too large write.")); + ssh2_deb((session, LIBSSH2_ERROR_STORE_OVERFLOW, + "Too large write.")); rc = LIBSSH2_ERROR_STORE_OVERFLOW; - LIBSSH2_FREE(session, *sig); + SSH2_FREE(session, *sig); *sig = NULL; *sig_len = 0; p = NULL; } } else { - _libssh2_debug((session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate ecdsa-sk signature.")); + ssh2_deb((session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate ecdsa-sk signature.")); rc = LIBSSH2_ERROR_ALLOC; } } else { /* sig, flags, counter, plus 4 bytes for sig length. */ *sig_len = 4 + sig_info.sig_r_len + 1 + 4; - *sig = LIBSSH2_ALLOC(session, *sig_len); + *sig = SSH2_ALLOC(session, *sig_len); if(*sig) { p = *sig; - - _libssh2_store_str(&p, - (const char *)sig_info.sig_r, - sig_info.sig_r_len); + ssh2_store_str(&p, (const char *)sig_info.sig_r, + sig_info.sig_r_len); } else { - _libssh2_debug((session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate ed25519-sk signature.")); + ssh2_deb((session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate ed25519-sk signature.")); rc = LIBSSH2_ERROR_ALLOC; } } @@ -1019,21 +1001,21 @@ int libssh2_sign_sk(LIBSSH2_SESSION *session, if(p) { *p = sig_info.flags; ++p; - _libssh2_store_u32(&p, sig_info.counter); + ssh2_store_u32(&p, sig_info.counter); *sig_len = p - *sig; } - LIBSSH2_FREE(session, sig_info.sig_r); + SSH2_FREE(session, sig_info.sig_r); if(sig_info.sig_s) { - LIBSSH2_FREE(session, sig_info.sig_s); + SSH2_FREE(session, sig_info.sig_s); } } else { - _libssh2_debug((session, LIBSSH2_ERROR_DECRYPT, - "sign_callback failed or " - "returned invalid signature.")); + ssh2_deb((session, LIBSSH2_ERROR_DECRYPT, + "sign_callback failed or " + "returned invalid signature.")); *sig_len = 0; } @@ -1056,7 +1038,7 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session, { int rc; - if(session->userauth_host_state == libssh2_NB_state_idle) { + if(session->userauth_host_state == ssh2_NB_state_idle) { const struct hostkey_method *privkeyobj; unsigned char *pubkeydata = NULL; unsigned char *sig = NULL; @@ -1075,18 +1057,18 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session, &session->userauth_host_method_len, &pubkeydata, &pubkeydata_len, publickey); if(rc) - /* Note: file_read_publickey() calls _libssh2_error() */ + /* Note: file_read_publickey() calls ssh2_err() */ return rc; } else { /* Compute public key from private key. */ - rc = _libssh2_pub_priv_keyfile(session, - &session->userauth_host_method, - &session->userauth_host_method_len, - &pubkeydata, &pubkeydata_len, - privatekey, passphrase); + rc = ssh2_pub_priv_keyfile(session, + &session->userauth_host_method, + &session->userauth_host_method_len, + &pubkeydata, &pubkeydata_len, + privatekey, passphrase); if(rc) - /* libssh2_pub_priv_keyfile() calls _libssh2_error() */ + /* ssh2_pub_priv_keyfile() calls ssh2_err() */ return rc; } @@ -1095,11 +1077,11 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session, hostname_len > MAX_INPUT_LEN || local_username_len > MAX_INPUT_LEN || pubkeydata_len > MAX_INPUT_LEN) { - LIBSSH2_FREE(session, session->userauth_host_method); + SSH2_FREE(session, session->userauth_host_method); session->userauth_host_method = NULL; - LIBSSH2_FREE(session, pubkeydata); - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Input parameter length too large"); + SSH2_FREE(session, pubkeydata); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Input parameter length too large"); } /* @@ -1118,47 +1100,46 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session, * the publickeydata itself */ session->userauth_host_s = session->userauth_host_packet = - LIBSSH2_ALLOC(session, - 4 + session->userauth_host_packet_len + - 4 + session->userauth_host_method_len + - 4 + pubkeydata_len); + SSH2_ALLOC(session, + 4 + session->userauth_host_packet_len + + 4 + session->userauth_host_method_len + + 4 + pubkeydata_len); if(!session->userauth_host_packet) { - LIBSSH2_FREE(session, session->userauth_host_method); + SSH2_FREE(session, session->userauth_host_method); session->userauth_host_method = NULL; - LIBSSH2_FREE(session, pubkeydata); - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Out of memory"); + SSH2_FREE(session, pubkeydata); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, "Out of memory"); } *(session->userauth_host_s++) = SSH_MSG_USERAUTH_REQUEST; - _libssh2_store_str(&session->userauth_host_s, username, username_len); - _libssh2_store_str(&session->userauth_host_s, "ssh-connection", 14); - _libssh2_store_str(&session->userauth_host_s, "hostbased", 9); - _libssh2_store_str(&session->userauth_host_s, - (const char *)session->userauth_host_method, - session->userauth_host_method_len); - _libssh2_store_str(&session->userauth_host_s, (const char *)pubkeydata, - pubkeydata_len); - LIBSSH2_FREE(session, pubkeydata); - _libssh2_store_str(&session->userauth_host_s, hostname, hostname_len); - _libssh2_store_str(&session->userauth_host_s, local_username, - local_username_len); + ssh2_store_str(&session->userauth_host_s, username, username_len); + ssh2_store_str(&session->userauth_host_s, "ssh-connection", 14); + ssh2_store_str(&session->userauth_host_s, "hostbased", 9); + ssh2_store_str(&session->userauth_host_s, + (const char *)session->userauth_host_method, + session->userauth_host_method_len); + ssh2_store_str(&session->userauth_host_s, (const char *)pubkeydata, + pubkeydata_len); + SSH2_FREE(session, pubkeydata); + ssh2_store_str(&session->userauth_host_s, hostname, hostname_len); + ssh2_store_str(&session->userauth_host_s, local_username, + local_username_len); rc = file_read_privatekey(session, &privkeyobj, &abstract, session->userauth_host_method, session->userauth_host_method_len, privatekey, passphrase); if(rc) { - /* Note: file_read_privatekey() calls _libssh2_error() */ - LIBSSH2_FREE(session, session->userauth_host_method); + /* Note: file_read_privatekey() calls ssh2_err() */ + SSH2_FREE(session, session->userauth_host_method); session->userauth_host_method = NULL; - LIBSSH2_FREE(session, session->userauth_host_packet); + SSH2_FREE(session, session->userauth_host_packet); session->userauth_host_packet = NULL; return rc; } - _libssh2_htonu32(buf, session->session_id_len); - libssh2_prepare_iovec(datavec, 4); + ssh2_htonu32(buf, session->session_id_len); + ssh2_prepare_iovec(datavec, 4); datavec[0].iov_base = (void *)buf; datavec[0].iov_len = 4; datavec[1].iov_base = (void *)session->session_id; @@ -1168,9 +1149,9 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session, if(privkeyobj && privkeyobj->signv && privkeyobj->signv(session, &sig, &sig_len, 3, datavec, &abstract)) { - LIBSSH2_FREE(session, session->userauth_host_method); + SSH2_FREE(session, session->userauth_host_method); session->userauth_host_method = NULL; - LIBSSH2_FREE(session, session->userauth_host_packet); + SSH2_FREE(session, session->userauth_host_packet); session->userauth_host_packet = NULL; if(privkeyobj->dtor) { privkeyobj->dtor(session, &abstract); @@ -1185,19 +1166,19 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session, if(sig_len > pubkeydata_len) { unsigned char *newpacket; /* Should *NEVER* happen, but...well.. better safe than sorry */ - newpacket = LIBSSH2_REALLOC(session, session->userauth_host_packet, - 4 + session->userauth_host_packet_len + - 4 + session->userauth_host_method_len + - 4 + sig_len); /* PK sigblob */ + newpacket = SSH2_REALLOC(session, session->userauth_host_packet, + 4 + session->userauth_host_packet_len + + 4 + session->userauth_host_method_len + + 4 + sig_len); /* PK sigblob */ if(!newpacket) { - LIBSSH2_FREE(session, sig); - LIBSSH2_FREE(session, session->userauth_host_packet); + SSH2_FREE(session, sig); + SSH2_FREE(session, session->userauth_host_packet); session->userauth_host_packet = NULL; - LIBSSH2_FREE(session, session->userauth_host_method); + SSH2_FREE(session, session->userauth_host_method); session->userauth_host_method = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed allocating additional space for " - "userauth-hostbased packet"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed allocating additional space for " + "userauth-hostbased packet"); } session->userauth_host_packet = newpacket; } @@ -1205,87 +1186,84 @@ static int userauth_hostbased_fromfile(LIBSSH2_SESSION *session, session->userauth_host_s = session->userauth_host_packet + session->userauth_host_packet_len; - _libssh2_store_u32(&session->userauth_host_s, - (uint32_t)(4 + session->userauth_host_method_len + - 4 + sig_len)); - _libssh2_store_str(&session->userauth_host_s, - (const char *)session->userauth_host_method, - session->userauth_host_method_len); - LIBSSH2_FREE(session, session->userauth_host_method); + ssh2_store_u32(&session->userauth_host_s, + (uint32_t)(4 + session->userauth_host_method_len + 4 + + sig_len)); + ssh2_store_str(&session->userauth_host_s, + (const char *)session->userauth_host_method, + session->userauth_host_method_len); + SSH2_FREE(session, session->userauth_host_method); session->userauth_host_method = NULL; - _libssh2_store_str(&session->userauth_host_s, (const char *)sig, - sig_len); - LIBSSH2_FREE(session, sig); + ssh2_store_str(&session->userauth_host_s, (const char *)sig, sig_len); + SSH2_FREE(session, sig); - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Attempting hostbased authentication")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Attempting hostbased authentication")); - session->userauth_host_state = libssh2_NB_state_created; + session->userauth_host_state = ssh2_NB_state_created; } - if(session->userauth_host_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, session->userauth_host_packet, - session->userauth_host_s - - session->userauth_host_packet, - NULL, 0); + if(session->userauth_host_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, session->userauth_host_packet, + session->userauth_host_s - + session->userauth_host_packet, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if(rc) { - LIBSSH2_FREE(session, session->userauth_host_packet); + SSH2_FREE(session, session->userauth_host_packet); session->userauth_host_packet = NULL; - session->userauth_host_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send userauth-hostbased request"); + session->userauth_host_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send userauth-hostbased request"); } - LIBSSH2_FREE(session, session->userauth_host_packet); + SSH2_FREE(session, session->userauth_host_packet); session->userauth_host_packet = NULL; - session->userauth_host_state = libssh2_NB_state_sent; + session->userauth_host_state = ssh2_NB_state_sent; } - if(session->userauth_host_state == libssh2_NB_state_sent) { + if(session->userauth_host_state == ssh2_NB_state_sent) { static const unsigned char reply_codes[3] = { SSH_MSG_USERAUTH_SUCCESS, SSH_MSG_USERAUTH_FAILURE, 0 }; size_t data_len; - rc = _libssh2_packet_requirev(session, reply_codes, - &session->userauth_host_data, - &data_len, 0, NULL, 0, - &session-> - userauth_host_packet_requirev_state); + rc = ssh2_packet_requirev(session, reply_codes, + &session->userauth_host_data, + &data_len, 0, NULL, 0, + &session-> + userauth_host_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } - session->userauth_host_state = libssh2_NB_state_idle; + session->userauth_host_state = ssh2_NB_state_idle; if(rc || data_len < 1) { - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Auth failed"); + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Auth failed"); } if(session->userauth_host_data[0] == SSH_MSG_USERAUTH_SUCCESS) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Hostbased authentication successful")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Hostbased authentication successful")); /* We are us and we have proved it. */ - LIBSSH2_FREE(session, session->userauth_host_data); + SSH2_FREE(session, session->userauth_host_data); session->userauth_host_data = NULL; - session->state |= LIBSSH2_STATE_AUTHENTICATED; + session->state |= SSH2_STATE_AUTHENTICATED; return 0; } } /* This public key is not allowed for this user on this server */ - LIBSSH2_FREE(session, session->userauth_host_data); + SSH2_FREE(session, session->userauth_host_data); session->userauth_host_data = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Invalid signature for supplied public key, or bad " - "username/public key combination"); + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Invalid signature for supplied public key, or bad " + "username/public key combination"); } /* @@ -1397,7 +1375,7 @@ static int is_version_less_than_78(const char *version) * @discussion Based on the incoming key_method value, this function * Upgrades the key method input based on user preferences, * server support algos and crypto backend support - * @related _libssh2_supported_key_sign_algorithms() + * @related ssh2_supported_key_sign_algs() * @param key_method current key method, usually the default key sig method * @param key_method_len length of the key method buffer * @result error code or zero on success @@ -1425,8 +1403,7 @@ static int key_sign_algorithm(LIBSSH2_SESSION *session, const char * const remote_ver_pre = "OpenSSH_"; const char *supported_algs = - _libssh2_supported_key_sign_algorithms(session, - *key_method, *key_method_len); + ssh2_supported_key_sign_algs(session, *key_method, *key_method_len); if(!supported_algs || !session->server_sign_algorithms) { /* no upgrading key algorithm supported, do nothing */ @@ -1452,11 +1429,11 @@ static int key_sign_algorithm(LIBSSH2_SESSION *session, } } - filtered_algs = LIBSSH2_ALLOC(session, - strlen(session->server_sign_algorithms) + 1); + filtered_algs = SSH2_ALLOC(session, + strlen(session->server_sign_algorithms) + 1); if(!filtered_algs) { - rc = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate filtered algs"); + rc = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate filtered algs"); return rc; } @@ -1524,8 +1501,8 @@ static int key_sign_algorithm(LIBSSH2_SESSION *session, if(match) { if(*key_method && *key_method_len == method_len && memcmp(*key_method, method, method_len) == 0) { - LIBSSH2_FREE(session, *key_method); - *key_method = LIBSSH2_ALLOC(session, match_len + suffix_len); + SSH2_FREE(session, *key_method); + *key_method = SSH2_ALLOC(session, match_len + suffix_len); if(*key_method) { memcpy(*key_method, match, match_len); memcpy(*key_method + match_len, suffix, suffix_len); @@ -1534,8 +1511,8 @@ static int key_sign_algorithm(LIBSSH2_SESSION *session, } else { if(*key_method) - LIBSSH2_FREE(session, *key_method); - *key_method = LIBSSH2_ALLOC(session, match_len); + SSH2_FREE(session, *key_method); + *key_method = SSH2_ALLOC(session, match_len); if(*key_method) { memcpy(*key_method, match, match_len); *key_method_len = match_len; @@ -1543,22 +1520,22 @@ static int key_sign_algorithm(LIBSSH2_SESSION *session, } if(!*key_method) { *key_method_len = 0; - rc = _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate key method upgrade"); + rc = ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate key method upgrade"); } } else { /* no match was found */ - rc = _libssh2_error(session, LIBSSH2_ERROR_METHOD_NONE, - "No signing signature matched"); + rc = ssh2_err(session, LIBSSH2_ERROR_METHOD_NONE, + "No signing signature matched"); } - LIBSSH2_FREE(session, filtered_algs); + SSH2_FREE(session, filtered_algs); return rc; } -int _libssh2_userauth_publickey( +int ssh2_userauth_publickey( LIBSSH2_SESSION *session, const char *username, size_t username_len, @@ -1580,15 +1557,15 @@ int _libssh2_userauth_publickey( retry_auth: auth_attempts++; - if(session->userauth_pblc_state == libssh2_NB_state_idle) { + if(session->userauth_pblc_state == ssh2_NB_state_idle) { /* - * The call to _libssh2_ntohu32 later relies on pubkeydata having at + * The call to ssh2_ntohu32() later relies on pubkeydata having at * least 4 valid bytes containing the length of the method name. */ if(pubkeydata_len < 4) - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Invalid public key, too short"); + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Invalid public key, too short"); /* * Cap caller-supplied input lengths early, before any allocation @@ -1597,8 +1574,8 @@ retry_auth: */ if(username_len > MAX_INPUT_LEN || pubkeydata_len > MAX_INPUT_LEN) - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Username or public key length too large"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Username or public key length too large"); /* Zero the whole thing out */ memset(&session->userauth_pblc_packet_requirev_state, 0, @@ -1611,22 +1588,21 @@ retry_auth: * For other uses, we allocate and populate it here. */ if(!session->userauth_pblc_method) { - session->userauth_pblc_method_len = _libssh2_ntohu32(pubkeydata); + session->userauth_pblc_method_len = ssh2_ntohu32(pubkeydata); if(session->userauth_pblc_method_len > MAX_INPUT_LEN || session->userauth_pblc_method_len > pubkeydata_len - 4) /* the method length cannot be longer than the entire passed in data, so we use this to detect crazy input data */ - return _libssh2_error(session, - LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Invalid public key"); + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Invalid public key"); session->userauth_pblc_method = - LIBSSH2_ALLOC(session, session->userauth_pblc_method_len); + SSH2_ALLOC(session, session->userauth_pblc_method_len); if(!session->userauth_pblc_method) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory " - "for public key data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory " + "for public key data"); } memcpy(session->userauth_pblc_method, pubkeydata + 4, session->userauth_pblc_method_len); @@ -1646,9 +1622,9 @@ retry_auth: if(session->userauth_pblc_method_len && session->userauth_pblc_method) { - _libssh2_debug((session, LIBSSH2_TRACE_KEX, "Signing using %.*s", - (int)session->userauth_pblc_method_len, - session->userauth_pblc_method)); + ssh2_deb((session, LIBSSH2_TRACE_KEX, "Signing using %.*s", + (int)session->userauth_pblc_method_len, + session->userauth_pblc_method)); } /* @@ -1671,166 +1647,161 @@ retry_auth: * this same allocation is reused. */ s = session->userauth_pblc_packet = - LIBSSH2_ALLOC(session, - 4 + session->userauth_pblc_packet_len + - 4 + session->userauth_pblc_method_len + - 4 + pubkeydata_len); + SSH2_ALLOC(session, + 4 + session->userauth_pblc_packet_len + + 4 + session->userauth_pblc_method_len + + 4 + pubkeydata_len); if(!session->userauth_pblc_packet) { - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Out of memory"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, "Out of memory"); } *s++ = SSH_MSG_USERAUTH_REQUEST; - _libssh2_store_str(&s, username, username_len); - _libssh2_store_str(&s, "ssh-connection", 14); - _libssh2_store_str(&s, "publickey", 9); + ssh2_store_str(&s, username, username_len); + ssh2_store_str(&s, "ssh-connection", 14); + ssh2_store_str(&s, "publickey", 9); session->userauth_pblc_b = s; /* Not sending signature with *this* packet */ *s++ = 0; - _libssh2_store_str(&s, (const char *)session->userauth_pblc_method, - session->userauth_pblc_method_len); - _libssh2_store_str(&s, (const char *)pubkeydata, pubkeydata_len); + ssh2_store_str(&s, (const char *)session->userauth_pblc_method, + session->userauth_pblc_method_len); + ssh2_store_str(&s, (const char *)pubkeydata, pubkeydata_len); - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Attempting publickey authentication")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Attempting publickey authentication")); - session->userauth_pblc_state = libssh2_NB_state_created; + session->userauth_pblc_state = ssh2_NB_state_created; } - if(session->userauth_pblc_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, session->userauth_pblc_packet, - session->userauth_pblc_packet_len, - NULL, 0); + if(session->userauth_pblc_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, session->userauth_pblc_packet, + session->userauth_pblc_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); else if(rc) { - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send userauth-publickey request"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send userauth-publickey request"); } - session->userauth_pblc_state = libssh2_NB_state_sent; + session->userauth_pblc_state = ssh2_NB_state_sent; } - if(session->userauth_pblc_state == libssh2_NB_state_sent) { - rc = _libssh2_packet_requirev(session, reply_codes, - &session->userauth_pblc_data, - &session->userauth_pblc_data_len, 0, - NULL, 0, - &session-> - userauth_pblc_packet_requirev_state); + if(session->userauth_pblc_state == ssh2_NB_state_sent) { + rc = ssh2_packet_requirev(session, reply_codes, + &session->userauth_pblc_data, + &session->userauth_pblc_data_len, 0, + NULL, 0, + &session-> + userauth_pblc_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if(rc || (session->userauth_pblc_data_len < 1)) { - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Waiting for USERAUTH response"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Waiting for USERAUTH response"); } if(session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_SUCCESS) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Pubkey authentication prematurely successful")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Pubkey authentication prematurely successful")); /* * God help any SSH server that allows an UNVERIFIED * public key to validate the user */ - LIBSSH2_FREE(session, session->userauth_pblc_data); + SSH2_FREE(session, session->userauth_pblc_data); session->userauth_pblc_data = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - session->state |= LIBSSH2_STATE_AUTHENTICATED; - session->userauth_pblc_state = libssh2_NB_state_idle; + session->state |= SSH2_STATE_AUTHENTICATED; + session->userauth_pblc_state = ssh2_NB_state_idle; return 0; } if(session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_FAILURE) { /* This public key is not allowed for this user on this server */ - LIBSSH2_FREE(session, session->userauth_pblc_data); + SSH2_FREE(session, session->userauth_pblc_data); session->userauth_pblc_data = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, - "Username/PublicKey combination invalid"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, + "Username/PublicKey combination invalid"); } /* Semi-Success! */ - LIBSSH2_FREE(session, session->userauth_pblc_data); + SSH2_FREE(session, session->userauth_pblc_data); session->userauth_pblc_data = NULL; *session->userauth_pblc_b = 0x01; - session->userauth_pblc_state = libssh2_NB_state_sent1; + session->userauth_pblc_state = ssh2_NB_state_sent1; } - if(session->userauth_pblc_state == libssh2_NB_state_sent1) { + if(session->userauth_pblc_state == ssh2_NB_state_sent1) { unsigned char *buf; unsigned char *sig = NULL; size_t sig_len; - s = buf = LIBSSH2_ALLOC(session, - 4 + session->session_id_len + - session->userauth_pblc_packet_len); + s = buf = SSH2_ALLOC(session, + 4 + session->session_id_len + + session->userauth_pblc_packet_len); if(!buf) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "userauth-publickey signed data"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "userauth-publickey signed data"); } - _libssh2_store_str(&s, (const char *)session->session_id, - session->session_id_len); + ssh2_store_str(&s, (const char *)session->session_id, + session->session_id_len); memcpy(s, session->userauth_pblc_packet, session->userauth_pblc_packet_len); s += session->userauth_pblc_packet_len; rc = sign_callback(session, &sig, &sig_len, buf, s - buf, abstract); - LIBSSH2_FREE(session, buf); + SSH2_FREE(session, buf); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if(rc == LIBSSH2_ERROR_ALGO_UNSUPPORTED && auth_attempts == 1) { /* try again with the default key algo */ - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; + session->userauth_pblc_state = ssh2_NB_state_idle; goto retry_auth; } else if(rc) { - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Callback returned error"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Callback returned error"); } if(!sig) { - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Callback did not return signature"); + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Callback did not return signature"); } /* @@ -1840,21 +1811,21 @@ retry_auth: if(sig_len > pubkeydata_len) { unsigned char *newpacket; /* Should *NEVER* happen, but...well.. better safe than sorry */ - newpacket = LIBSSH2_REALLOC(session, - session->userauth_pblc_packet, - 4 + session->userauth_pblc_packet_len + - 4 + session->userauth_pblc_method_len + - 4 + sig_len); /* PK sigblob */ + newpacket = SSH2_REALLOC(session, + session->userauth_pblc_packet, + 4 + session->userauth_pblc_packet_len + + 4 + session->userauth_pblc_method_len + + 4 + sig_len); /* PK sigblob */ if(!newpacket) { - LIBSSH2_FREE(session, sig); - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, sig); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Failed allocating additional space for " - "userauth-publickey packet"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Failed allocating additional space for " + "userauth-publickey packet"); } session->userauth_pblc_packet = newpacket; } @@ -1872,93 +1843,92 @@ retry_auth: strncmp((const char *)session->userauth_pblc_method, "sk-ssh-ed25519@openssh.com", session->userauth_pblc_method_len) == 0) { - _libssh2_store_u32(&s, - (uint32_t)(4 + session->userauth_pblc_method_len + - sig_len)); - _libssh2_store_str(&s, (const char *)session->userauth_pblc_method, - session->userauth_pblc_method_len); + ssh2_store_u32(&s, + (uint32_t)(4 + session->userauth_pblc_method_len + + sig_len)); + ssh2_store_str(&s, (const char *)session->userauth_pblc_method, + session->userauth_pblc_method_len); memcpy(s, sig, sig_len); s += sig_len; } else { - _libssh2_store_u32(&s, - (uint32_t)(4 + session->userauth_pblc_method_len + - 4 + sig_len)); - _libssh2_store_str(&s, (const char *)session->userauth_pblc_method, - session->userauth_pblc_method_len); - _libssh2_store_str(&s, (const char *)sig, sig_len); + ssh2_store_u32(&s, + (uint32_t)(4 + session->userauth_pblc_method_len + + 4 + sig_len)); + ssh2_store_str(&s, (const char *)session->userauth_pblc_method, + session->userauth_pblc_method_len); + ssh2_store_str(&s, (const char *)sig, sig_len); } - LIBSSH2_FREE(session, session->userauth_pblc_method); + SSH2_FREE(session, session->userauth_pblc_method); session->userauth_pblc_method = NULL; - LIBSSH2_FREE(session, sig); + SSH2_FREE(session, sig); - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Attempting publickey authentication -- phase 2")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Attempting publickey authentication -- phase 2")); session->userauth_pblc_s = s; - session->userauth_pblc_state = libssh2_NB_state_sent2; + session->userauth_pblc_state = ssh2_NB_state_sent2; } - if(session->userauth_pblc_state == libssh2_NB_state_sent2) { - rc = _libssh2_transport_send(session, session->userauth_pblc_packet, - session->userauth_pblc_s - - session->userauth_pblc_packet, - NULL, 0); + if(session->userauth_pblc_state == ssh2_NB_state_sent2) { + rc = ssh2_transport_send(session, session->userauth_pblc_packet, + session->userauth_pblc_s - + session->userauth_pblc_packet, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if(rc) { - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send userauth-publickey request"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send userauth-publickey request"); } - LIBSSH2_FREE(session, session->userauth_pblc_packet); + SSH2_FREE(session, session->userauth_pblc_packet); session->userauth_pblc_packet = NULL; - session->userauth_pblc_state = libssh2_NB_state_sent3; + session->userauth_pblc_state = ssh2_NB_state_sent3; } /* PK_OK is no longer valid */ reply_codes[2] = 0; - rc = _libssh2_packet_requirev(session, reply_codes, - &session->userauth_pblc_data, - &session->userauth_pblc_data_len, 0, NULL, 0, - &session->userauth_pblc_packet_requirev_state); + rc = ssh2_packet_requirev(session, reply_codes, + &session->userauth_pblc_data, + &session->userauth_pblc_data_len, 0, NULL, 0, + &session->userauth_pblc_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block waiting for publickey " - "USERAUTH response"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, + "Would block waiting for publickey " + "USERAUTH response"); } else if(rc || session->userauth_pblc_data_len < 1) { - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Waiting for publickey USERAUTH response"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Waiting for publickey USERAUTH response"); } if(session->userauth_pblc_data[0] == SSH_MSG_USERAUTH_SUCCESS) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Publickey authentication successful")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Publickey authentication successful")); /* We are us and we have proved it. */ - LIBSSH2_FREE(session, session->userauth_pblc_data); + SSH2_FREE(session, session->userauth_pblc_data); session->userauth_pblc_data = NULL; - session->state |= LIBSSH2_STATE_AUTHENTICATED; - session->userauth_pblc_state = libssh2_NB_state_idle; + session->state |= SSH2_STATE_AUTHENTICATED; + session->userauth_pblc_state = ssh2_NB_state_idle; return 0; } /* This public key is not allowed for this user on this server */ - LIBSSH2_FREE(session, session->userauth_pblc_data); + SSH2_FREE(session, session->userauth_pblc_data); session->userauth_pblc_data = NULL; - session->userauth_pblc_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, - "Invalid signature for supplied public key, or bad " - "username/public key combination"); + session->userauth_pblc_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED, + "Invalid signature for supplied public key, or bad " + "username/public key combination"); } /* @@ -1983,7 +1953,7 @@ static int userauth_publickey_frommemory(LIBSSH2_SESSION *session, privkey_mem.data_len = privatekeydata_len; privkey_mem.passphrase = passphrase; - if(session->userauth_pblc_state == libssh2_NB_state_idle) { + if(session->userauth_pblc_state == ssh2_NB_state_idle) { if(publickeydata_len && publickeydata) { rc = memory_read_publickey(session, &session->userauth_pblc_method, &session->userauth_pblc_method_len, @@ -1994,7 +1964,7 @@ static int userauth_publickey_frommemory(LIBSSH2_SESSION *session, } else if(privatekeydata_len && privatekeydata) { /* Compute public key from private key. */ - rc = _libssh2_pub_priv_keyfilememory(session, + rc = ssh2_pub_priv_keyfilememory(session, &session->userauth_pblc_method, &session->userauth_pblc_method_len, &pubkeydata, &pubkeydata_len, @@ -2004,16 +1974,16 @@ static int userauth_publickey_frommemory(LIBSSH2_SESSION *session, return rc; } else { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid data in public and private key."); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid data in public and private key."); } } - rc = _libssh2_userauth_publickey(session, username, username_len, - pubkeydata, pubkeydata_len, - sign_frommemory, &abstract); + rc = ssh2_userauth_publickey(session, username, username_len, + pubkeydata, pubkeydata_len, + sign_frommemory, &abstract); if(pubkeydata) - LIBSSH2_FREE(session, pubkeydata); + SSH2_FREE(session, pubkeydata); return rc; } @@ -2037,7 +2007,7 @@ static int userauth_publickey_fromfile(LIBSSH2_SESSION *session, privkey_file.filename = privatekey; privkey_file.passphrase = passphrase; - if(session->userauth_pblc_state == libssh2_NB_state_idle) { + if(session->userauth_pblc_state == ssh2_NB_state_idle) { if(publickey) { rc = file_read_publickey(session, &session->userauth_pblc_method, &session->userauth_pblc_method_len, @@ -2047,23 +2017,23 @@ static int userauth_publickey_fromfile(LIBSSH2_SESSION *session, } else { /* Compute public key from private key. */ - rc = _libssh2_pub_priv_keyfile(session, - &session->userauth_pblc_method, - &session->userauth_pblc_method_len, - &pubkeydata, &pubkeydata_len, - privatekey, passphrase); + rc = ssh2_pub_priv_keyfile(session, + &session->userauth_pblc_method, + &session->userauth_pblc_method_len, + &pubkeydata, &pubkeydata_len, + privatekey, passphrase); - /* _libssh2_pub_priv_keyfile calls _libssh2_error() */ + /* ssh2_pub_priv_keyfile() calls ssh2_err() */ if(rc) return rc; } } - rc = _libssh2_userauth_publickey(session, username, username_len, - pubkeydata, pubkeydata_len, - sign_fromfile, &abstract); + rc = ssh2_userauth_publickey(session, username, username_len, + pubkeydata, pubkeydata_len, + sign_fromfile, &abstract); if(pubkeydata) - LIBSSH2_FREE(session, pubkeydata); + SSH2_FREE(session, pubkeydata); return rc; } @@ -2142,10 +2112,10 @@ int libssh2_userauth_publickey( return LIBSSH2_ERROR_BAD_USE; BLOCK_ADJUST(rc, session, - _libssh2_userauth_publickey(session, - username, strlen(username), - pubkeydata, pubkeydata_len, - sign_callback, abstract)); + ssh2_userauth_publickey(session, + username, strlen(username), + pubkeydata, pubkeydata_len, + sign_callback, abstract)); return rc; } @@ -2167,7 +2137,7 @@ static int userauth_keyboard_interactive( }; unsigned int i; - if(session->userauth_kybd_state == libssh2_NB_state_idle) { + if(session->userauth_kybd_state == ssh2_NB_state_idle) { session->userauth_kybd_auth_name = NULL; session->userauth_kybd_auth_instruction = NULL; session->userauth_kybd_num_prompts = 0; @@ -2180,8 +2150,7 @@ static int userauth_keyboard_interactive( sizeof(session->userauth_kybd_packet_requirev_state)); if(username_len > MAX_INPUT_LEN) { - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Username too long"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, "Username too long"); } session->userauth_kybd_packet_len = @@ -2196,99 +2165,91 @@ static int userauth_keyboard_interactive( ; session->userauth_kybd_data = s = - LIBSSH2_ALLOC(session, session->userauth_kybd_packet_len); + SSH2_ALLOC(session, session->userauth_kybd_packet_len); if(!s) { - return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "keyboard-interactive authentication"); + return ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "keyboard-interactive authentication"); } *s++ = SSH_MSG_USERAUTH_REQUEST; /* username */ - _libssh2_store_str(&s, username, username_len); + ssh2_store_str(&s, username, username_len); /* service name */ - _libssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1); + ssh2_store_str(&s, "ssh-connection", sizeof("ssh-connection") - 1); /* "keyboard-interactive" */ - _libssh2_store_str(&s, "keyboard-interactive", - sizeof("keyboard-interactive") - 1); + ssh2_store_str(&s, "keyboard-interactive", + sizeof("keyboard-interactive") - 1); /* language tag */ - _libssh2_store_u32(&s, 0); + ssh2_store_u32(&s, 0); /* submethods */ - _libssh2_store_u32(&s, 0); + ssh2_store_u32(&s, 0); - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Attempting keyboard-interactive authentication")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Attempting keyboard-interactive authentication")); - session->userauth_kybd_state = libssh2_NB_state_created; + session->userauth_kybd_state = ssh2_NB_state_created; } - if(session->userauth_kybd_state == libssh2_NB_state_created) { - rc = _libssh2_transport_send(session, session->userauth_kybd_data, - session->userauth_kybd_packet_len, - NULL, 0); + if(session->userauth_kybd_state == ssh2_NB_state_created) { + rc = ssh2_transport_send(session, session->userauth_kybd_data, + session->userauth_kybd_packet_len, NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if(rc) { - LIBSSH2_FREE(session, session->userauth_kybd_data); + SSH2_FREE(session, session->userauth_kybd_data); session->userauth_kybd_data = NULL; - session->userauth_kybd_state = libssh2_NB_state_idle; - return _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send keyboard-interactive" - " request"); + session->userauth_kybd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send keyboard-interactive request"); } - LIBSSH2_FREE(session, session->userauth_kybd_data); + SSH2_FREE(session, session->userauth_kybd_data); session->userauth_kybd_data = NULL; - session->userauth_kybd_state = libssh2_NB_state_sent; + session->userauth_kybd_state = ssh2_NB_state_sent; } for(;;) { - if(session->userauth_kybd_state == libssh2_NB_state_sent) { - rc = _libssh2_packet_requirev(session, reply_codes, - &session->userauth_kybd_data, - &session->userauth_kybd_data_len, - 0, NULL, 0, - &session-> - userauth_kybd_packet_requirev_state); + if(session->userauth_kybd_state == ssh2_NB_state_sent) { + rc = ssh2_packet_requirev(session, reply_codes, + &session->userauth_kybd_data, + &session->userauth_kybd_data_len, + 0, NULL, 0, + &session-> + userauth_kybd_packet_requirev_state); if(rc == LIBSSH2_ERROR_EAGAIN) { - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); } else if(rc || session->userauth_kybd_data_len < 1) { - session->userauth_kybd_state = libssh2_NB_state_idle; - return _libssh2_error(session, - LIBSSH2_ERROR_AUTHENTICATION_FAILED, - "Waiting for keyboard " - "USERAUTH response"); + session->userauth_kybd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, + "Waiting for keyboard USERAUTH response"); } if(session->userauth_kybd_data[0] == SSH_MSG_USERAUTH_SUCCESS) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Keyboard-interactive " - "authentication successful")); - LIBSSH2_FREE(session, session->userauth_kybd_data); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Keyboard-interactive authentication successful")); + SSH2_FREE(session, session->userauth_kybd_data); session->userauth_kybd_data = NULL; - session->state |= LIBSSH2_STATE_AUTHENTICATED; - session->userauth_kybd_state = libssh2_NB_state_idle; + session->state |= SSH2_STATE_AUTHENTICATED; + session->userauth_kybd_state = ssh2_NB_state_idle; return 0; } if(session->userauth_kybd_data[0] == SSH_MSG_USERAUTH_FAILURE) { - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Keyboard-interactive authentication failed")); - LIBSSH2_FREE(session, session->userauth_kybd_data); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Keyboard-interactive authentication failed")); + SSH2_FREE(session, session->userauth_kybd_data); session->userauth_kybd_data = NULL; - session->userauth_kybd_state = libssh2_NB_state_idle; - return _libssh2_error(session, - LIBSSH2_ERROR_AUTHENTICATION_FAILED, - "Authentication failed " - "(keyboard-interactive)"); + session->userauth_kybd_state = ssh2_NB_state_idle; + return ssh2_err(session, LIBSSH2_ERROR_AUTHENTICATION_FAILED, + "Authentication failed " + "(keyboard-interactive)"); } /* server requested PAM-like conversation */ @@ -2307,9 +2268,9 @@ static int userauth_keyboard_interactive( session->userauth_kybd_responses, &session->abstract); - _libssh2_debug((session, LIBSSH2_TRACE_AUTH, - "Keyboard-interactive response callback function" - " invoked")); + ssh2_deb((session, LIBSSH2_TRACE_AUTH, + "Keyboard-interactive response callback function" + " invoked")); session->userauth_kybd_packet_len = 1 /* byte SSH_MSG_USERAUTH_INFO_RESPONSE */ @@ -2324,50 +2285,48 @@ static int userauth_keyboard_interactive( 4 + (size_t)session->userauth_kybd_responses[i].length; } else { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for keyboard-" - "interactive response packet"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for keyboard-" + "interactive response packet"); goto cleanup; } } /* A new userauth_kybd_data area is to be allocated, free the former one. */ - LIBSSH2_FREE(session, session->userauth_kybd_data); + SSH2_FREE(session, session->userauth_kybd_data); session->userauth_kybd_data = s = - LIBSSH2_ALLOC(session, session->userauth_kybd_packet_len); + SSH2_ALLOC(session, session->userauth_kybd_packet_len); if(!s) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for keyboard-" - "interactive response packet"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for keyboard-" + "interactive response packet"); goto cleanup; } *s = SSH_MSG_USERAUTH_INFO_RESPONSE; s++; - _libssh2_store_u32(&s, session->userauth_kybd_num_prompts); + ssh2_store_u32(&s, session->userauth_kybd_num_prompts); for(i = 0; i < session->userauth_kybd_num_prompts; i++) { - _libssh2_store_str(&s, - session->userauth_kybd_responses[i].text, - session->userauth_kybd_responses[i].length); + ssh2_store_str(&s, session->userauth_kybd_responses[i].text, + session->userauth_kybd_responses[i].length); } - session->userauth_kybd_state = libssh2_NB_state_sent1; + session->userauth_kybd_state = ssh2_NB_state_sent1; } - if(session->userauth_kybd_state == libssh2_NB_state_sent1) { - rc = _libssh2_transport_send(session, session->userauth_kybd_data, - session->userauth_kybd_packet_len, - NULL, 0); + if(session->userauth_kybd_state == ssh2_NB_state_sent1) { + rc = ssh2_transport_send(session, session->userauth_kybd_data, + session->userauth_kybd_packet_len, + NULL, 0); if(rc == LIBSSH2_ERROR_EAGAIN) - return _libssh2_error(session, LIBSSH2_ERROR_EAGAIN, - "Would block"); + return ssh2_err(session, LIBSSH2_ERROR_EAGAIN, "Would block"); if(rc) { - _libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND, - "Unable to send userauth-keyboard-interactive" - " request"); + ssh2_err(session, LIBSSH2_ERROR_SOCKET_SEND, + "Unable to send userauth-keyboard-interactive" + " request"); goto cleanup; } @@ -2380,47 +2339,46 @@ cleanup: * are filled by zeroes */ - LIBSSH2_FREE(session, session->userauth_kybd_data); + SSH2_FREE(session, session->userauth_kybd_data); session->userauth_kybd_data = NULL; if(session->userauth_kybd_prompts) { for(i = 0; i < session->userauth_kybd_num_prompts; i++) { - LIBSSH2_FREE(session, session->userauth_kybd_prompts[i].text); + SSH2_FREE(session, session->userauth_kybd_prompts[i].text); session->userauth_kybd_prompts[i].text = NULL; } } if(session->userauth_kybd_responses) { for(i = 0; i < session->userauth_kybd_num_prompts; i++) { - LIBSSH2_FREE(session, - session->userauth_kybd_responses[i].text); + SSH2_FREE(session, session->userauth_kybd_responses[i].text); session->userauth_kybd_responses[i].text = NULL; } } if(session->userauth_kybd_prompts) { - LIBSSH2_FREE(session, session->userauth_kybd_prompts); + SSH2_FREE(session, session->userauth_kybd_prompts); session->userauth_kybd_prompts = NULL; } if(session->userauth_kybd_responses) { - LIBSSH2_FREE(session, session->userauth_kybd_responses); + SSH2_FREE(session, session->userauth_kybd_responses); session->userauth_kybd_responses = NULL; } if(session->userauth_kybd_auth_name) { - LIBSSH2_FREE(session, session->userauth_kybd_auth_name); + SSH2_FREE(session, session->userauth_kybd_auth_name); session->userauth_kybd_auth_name = NULL; } if(session->userauth_kybd_auth_instruction) { - LIBSSH2_FREE(session, session->userauth_kybd_auth_instruction); + SSH2_FREE(session, session->userauth_kybd_auth_instruction); session->userauth_kybd_auth_instruction = NULL; } if(session->userauth_kybd_auth_failure) { - session->userauth_kybd_state = libssh2_NB_state_idle; + session->userauth_kybd_state = ssh2_NB_state_idle; return -1; } - session->userauth_kybd_state = libssh2_NB_state_sent; + session->userauth_kybd_state = ssh2_NB_state_sent; } } @@ -2472,21 +2430,20 @@ int libssh2_userauth_publickey_sk( if(privatekeydata_len && privatekeydata) { - if(_libssh2_sk_pub_keyfilememory(session, - &tmp_method, - &tmp_method_len, - &tmp_publickeydata, - &tmp_publickeydata_len, - &(sk_info.algorithm), - &(sk_info.flags), - &(sk_info.application), - &(sk_info.key_handle), - &(sk_info.handle_len), - privatekeydata, privatekeydata_len, - passphrase)) { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public key " - "from private key."); + if(ssh2_sk_pub_keyfilememory(session, + &tmp_method, + &tmp_method_len, + &tmp_publickeydata, + &tmp_publickeydata_len, + &(sk_info.algorithm), + &(sk_info.flags), + &(sk_info.application), + &(sk_info.key_handle), + &(sk_info.handle_len), + privatekeydata, privatekeydata_len, + passphrase)) { + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public key from private key."); } else if(publickeydata_len == 0 || !publickeydata) { session->userauth_pblc_method = tmp_method; @@ -2500,13 +2457,13 @@ int libssh2_userauth_publickey_sk( const char *ed25519 = "sk-ssh-ed25519-cert-v01@openssh.com"; if(tmp_method) { - LIBSSH2_FREE(session, tmp_method); + SSH2_FREE(session, tmp_method); } if(!strncmp((const char *)publickeydata, ecdsa, strlen(ecdsa))) { session->userauth_pblc_method_len = strlen(ecdsa); session->userauth_pblc_method = - LIBSSH2_ALLOC(session, session->userauth_pblc_method_len); + SSH2_ALLOC(session, session->userauth_pblc_method_len); memcpy(session->userauth_pblc_method, ecdsa, session->userauth_pblc_method_len); @@ -2515,7 +2472,7 @@ int libssh2_userauth_publickey_sk( strlen(ed25519))) { session->userauth_pblc_method_len = strlen(ed25519); session->userauth_pblc_method = - LIBSSH2_ALLOC(session, session->userauth_pblc_method_len); + SSH2_ALLOC(session, session->userauth_pblc_method_len); memcpy(session->userauth_pblc_method, ed25519, session->userauth_pblc_method_len); @@ -2530,27 +2487,27 @@ int libssh2_userauth_publickey_sk( } } else { - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Invalid data in public and private key."); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Invalid data in public and private key."); } if(rc == LIBSSH2_ERROR_NONE) { - rc = _libssh2_userauth_publickey(session, username, username_len, - pubkeydata, pubkeydata_len, - libssh2_sign_sk, &sign_abstract); + rc = ssh2_userauth_publickey(session, username, username_len, + pubkeydata, pubkeydata_len, + libssh2_sign_sk, &sign_abstract); while(rc == LIBSSH2_ERROR_EAGAIN) { - rc = _libssh2_userauth_publickey(session, username, username_len, - pubkeydata, pubkeydata_len, - libssh2_sign_sk, &sign_abstract); + rc = ssh2_userauth_publickey(session, username, username_len, + pubkeydata, pubkeydata_len, + libssh2_sign_sk, &sign_abstract); } } if(tmp_publickeydata) - LIBSSH2_FREE(session, tmp_publickeydata); + SSH2_FREE(session, tmp_publickeydata); if(sk_info.application) { - LIBSSH2_FREE(session, (void *)LIBSSH2_UNCONST(sk_info.application)); + SSH2_FREE(session, SSH2_UNCONST(sk_info.application)); } return rc; diff --git a/src/userauth.h b/src/userauth.h index cd64875e..b8347570 100644 --- a/src/userauth.h +++ b/src/userauth.h @@ -40,7 +40,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -int _libssh2_userauth_publickey( +int ssh2_userauth_publickey( LIBSSH2_SESSION *session, const char *username, size_t username_len, diff --git a/src/userauth_kbd_packet.c b/src/userauth_kbd_packet.c index 3623f8ca..fde499d8 100644 --- a/src/userauth_kbd_packet.c +++ b/src/userauth_kbd_packet.c @@ -55,67 +55,61 @@ int userauth_keyboard_interactive_decode_info_request(LIBSSH2_SESSION *session) decoded.len = session->userauth_kybd_data_len; if(session->userauth_kybd_data_len < 17) { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "userauth keyboard data buffer too small " - "to get length"); + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "userauth keyboard data buffer too small to get length"); return -1; } /* byte SSH_MSG_USERAUTH_INFO_REQUEST */ - _libssh2_get_byte(&decoded, &packet_type); + ssh2_get_byte(&decoded, &packet_type); /* string name (ISO-10646 UTF-8) */ - if(_libssh2_copy_string(session, &decoded, - &session->userauth_kybd_auth_name, - &session->userauth_kybd_auth_name_len) == -1) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to decode " - "keyboard-interactive 'name' " - "request field"); + if(ssh2_copy_string(session, &decoded, + &session->userauth_kybd_auth_name, + &session->userauth_kybd_auth_name_len) == -1) { + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to decode keyboard-interactive 'name' " + "request field"); return -1; } /* string instruction (ISO-10646 UTF-8) */ - if(_libssh2_copy_string(session, &decoded, - &session->userauth_kybd_auth_instruction, - &session->userauth_kybd_auth_instruction_len) - == -1) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to decode " - "keyboard-interactive 'instruction' " - "request field"); + if(ssh2_copy_string(session, &decoded, + &session->userauth_kybd_auth_instruction, + &session->userauth_kybd_auth_instruction_len) == -1) { + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to decode keyboard-interactive 'instruction' " + "request field"); return -1; } /* string language tag (as defined in [RFC-3066]) */ - if(_libssh2_get_string(&decoded, &language_tag, &language_tag_len) == -1) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to decode " - "keyboard-interactive 'language tag' " - "request field"); + if(ssh2_get_string(&decoded, &language_tag, &language_tag_len) == -1) { + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to decode keyboard-interactive 'language tag' " + "request field"); return -1; } /* int num-prompts */ - if(_libssh2_get_u32(&decoded, &tmp_u32) == -1) { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Unable to decode " - "keyboard-interactive number of keyboard prompts"); + if(ssh2_get_u32(&decoded, &tmp_u32) == -1) { + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Unable to decode " + "keyboard-interactive number of keyboard prompts"); return -1; } session->userauth_kybd_num_prompts = tmp_u32; if(session->userauth_kybd_num_prompts != tmp_u32) { - _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Unable to decode " - "keyboard-interactive number of keyboard prompts"); + ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Unable to decode " + "keyboard-interactive number of keyboard prompts"); return -1; } if(session->userauth_kybd_num_prompts > 100) { - _libssh2_error(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, - "Too many replies for " - "keyboard-interactive prompts"); + ssh2_err(session, LIBSSH2_ERROR_OUT_OF_BOUNDARY, + "Too many replies for keyboard-interactive prompts"); return -1; } @@ -124,46 +118,40 @@ int userauth_keyboard_interactive_decode_info_request(LIBSSH2_SESSION *session) } session->userauth_kybd_prompts = - LIBSSH2_CALLOC(session, - sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) * - session->userauth_kybd_num_prompts); + SSH2_CALLOC(session, sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) * + session->userauth_kybd_num_prompts); if(!session->userauth_kybd_prompts) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "keyboard-interactive prompts array"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "keyboard-interactive prompts array"); return -1; } session->userauth_kybd_responses = - LIBSSH2_CALLOC(session, - sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) * - session->userauth_kybd_num_prompts); + SSH2_CALLOC(session, sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) * + session->userauth_kybd_num_prompts); if(!session->userauth_kybd_responses) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to allocate memory for " - "keyboard-interactive responses array"); + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to allocate memory for " + "keyboard-interactive responses array"); return -1; } for(i = 0; i < session->userauth_kybd_num_prompts; i++) { /* string prompt[1] (ISO-10646 UTF-8) */ - if(_libssh2_copy_string(session, &decoded, - &session->userauth_kybd_prompts[i].text, - &session->userauth_kybd_prompts[i].length) - == -1) { - _libssh2_error(session, LIBSSH2_ERROR_ALLOC, - "Unable to decode " - "keyboard-interactive prompt message"); + if(ssh2_copy_string(session, &decoded, + &session->userauth_kybd_prompts[i].text, + &session->userauth_kybd_prompts[i].length) == -1) { + ssh2_err(session, LIBSSH2_ERROR_ALLOC, + "Unable to decode keyboard-interactive prompt message"); return -1; } /* boolean echo[1] */ - if(_libssh2_get_boolean(&decoded, - &session->userauth_kybd_prompts[i].echo) - == -1) { - _libssh2_error(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, - "Unable to decode " - "user auth keyboard prompt echo"); + if(ssh2_get_boolean(&decoded, + &session->userauth_kybd_prompts[i].echo) == -1) { + ssh2_err(session, LIBSSH2_ERROR_BUFFER_TOO_SMALL, + "Unable to decode user auth keyboard prompt echo"); return -1; } } diff --git a/src/version.c b/src/version.c index d32ae64a..90242434 100644 --- a/src/version.c +++ b/src/version.c @@ -50,5 +50,5 @@ const char *libssh2_version(int req_version_num) LIBSSH2_API libssh2_crypto_engine_t libssh2_crypto_engine(void) { - return LIBSSH2_CRYPTO_ENGINE; + return SSH2_CRYPTO_ENGINE; } diff --git a/src/wincng.c b/src/wincng.c index d02db1df..c53ac61d 100644 --- a/src/wincng.c +++ b/src/wincng.c @@ -118,13 +118,13 @@ #define PKCS_RSA_PRIVATE_KEY ((LPCSTR)(size_t)43) #endif -static void wincng_safe_free(void *buf, size_t len) +static void wcng_safe_free(void *buf, size_t len) { if(!buf) return; if(len > 0) - _libssh2_explicit_zero(buf, len); + ssh2_explicit_zero(buf, len); free(buf); } @@ -132,8 +132,8 @@ static void wincng_safe_free(void *buf, size_t len) /* Copy a big endian set of bits from src to dest. * if the size of src is smaller than dest then pad the "left" (MSB) * end with zeroes and copy the bits into the "right" (LSB) end. */ -static void memcpy_with_be_padding(unsigned char *dest, ULONG dest_len, - unsigned char *src, ULONG src_len) +static void wcng_memcpy_with_be_padding(unsigned char *dest, ULONG dest_len, + unsigned char *src, ULONG src_len) { if(dest_len > src_len) { memset(dest, 0, dest_len - src_len); @@ -146,11 +146,11 @@ static void memcpy_with_be_padding(unsigned char *dest, ULONG dest_len, * Windows CNG backend: BigNumber functions */ -libssh2_bn *_libssh2_wincng_bignum_init(void) +ssh2_bn *ssh2_wcng_bn_init(void) { - libssh2_bn *bignum; + ssh2_bn *bignum; - bignum = (libssh2_bn *)malloc(sizeof(libssh2_bn)); + bignum = (ssh2_bn *)malloc(sizeof(ssh2_bn)); if(bignum) { bignum->bignum = NULL; bignum->length = 0; @@ -159,7 +159,7 @@ libssh2_bn *_libssh2_wincng_bignum_init(void) return bignum; } -static int wincng_bignum_resize(libssh2_bn *bn, ULONG length) +static int wcng_bn_resize(ssh2_bn *bn, ULONG length) { unsigned char *bignum; @@ -170,7 +170,7 @@ static int wincng_bignum_resize(libssh2_bn *bn, ULONG length) return 0; if(bn->bignum && bn->length > 0 && length < bn->length) { - _libssh2_explicit_zero(bn->bignum + length, bn->length - length); + ssh2_explicit_zero(bn->bignum + length, bn->length - length); } bignum = realloc(bn->bignum, length); @@ -183,7 +183,7 @@ static int wincng_bignum_resize(libssh2_bn *bn, ULONG length) return 0; } -static int wincng_bignum_rand(libssh2_bn *rnd, int bits, int top, int bottom) +static int wcng_bn_random(ssh2_bn *rnd, int bits, int top, int bottom) { unsigned char *bignum; ULONG length; @@ -192,7 +192,7 @@ static int wincng_bignum_rand(libssh2_bn *rnd, int bits, int top, int bottom) return -1; length = (ULONG)(ceil(((double)bits) / 8.0) * sizeof(unsigned char)); - if(wincng_bignum_resize(rnd, length)) + if(wcng_bn_resize(rnd, length)) return -1; bignum = rnd->bignum; @@ -200,7 +200,7 @@ static int wincng_bignum_rand(libssh2_bn *rnd, int bits, int top, int bottom) if(!bignum) return -1; - if(_libssh2_wincng_random(bignum, length)) + if(ssh2_wcng_random(bignum, length)) return -1; /* calculate significant bits in most significant byte */ @@ -224,10 +224,7 @@ static int wincng_bignum_rand(libssh2_bn *rnd, int bits, int top, int bottom) return 0; } -static int wincng_bignum_mod_exp(libssh2_bn *r, - libssh2_bn *a, - libssh2_bn *p, - libssh2_bn *m) +static int wcng_bn_mod_exp(ssh2_bn *r, ssh2_bn *a, ssh2_bn *p, ssh2_bn *m) { BCRYPT_KEY_HANDLE hKey; BCRYPT_RSAKEY_BLOB *rsakey; @@ -259,28 +256,27 @@ static int wincng_bignum_mod_exp(libssh2_bn *r, memcpy((unsigned char *)rsakey + offset, m->bignum, m->length); offset = 0; - ret = BCryptImportKeyPair(wincng_ctx.hAlgRSA, NULL, - BCRYPT_RSAPUBLIC_BLOB, &hKey, - (PUCHAR)rsakey, keylen, 0); + ret = BCryptImportKeyPair(ssh2_wcng.hAlgRSA, NULL, BCRYPT_RSAPUBLIC_BLOB, + &hKey, (PUCHAR)rsakey, keylen, 0); if(BCRYPT_SUCCESS(ret)) { ret = BCryptEncrypt(hKey, a->bignum, a->length, NULL, NULL, 0, NULL, 0, &length, BCRYPT_PAD_NONE); if(BCRYPT_SUCCESS(ret)) { - if(!wincng_bignum_resize(r, length)) { + if(!wcng_bn_resize(r, length)) { length = max(a->length, length); bignum = malloc(length); if(bignum) { - memcpy_with_be_padding(bignum, length, - a->bignum, a->length); + wcng_memcpy_with_be_padding(bignum, length, + a->bignum, a->length); ret = BCryptEncrypt(hKey, bignum, length, NULL, NULL, 0, r->bignum, r->length, &offset, BCRYPT_PAD_NONE); - wincng_safe_free(bignum, length); + wcng_safe_free(bignum, length); if(BCRYPT_SUCCESS(ret)) { - wincng_bignum_resize(r, offset); + wcng_bn_resize(r, offset); } } else @@ -293,12 +289,12 @@ static int wincng_bignum_mod_exp(libssh2_bn *r, BCryptDestroyKey(hKey); } - wincng_safe_free(rsakey, keylen); + wcng_safe_free(rsakey, keylen); return BCRYPT_SUCCESS(ret) ? 0 : -1; } -int _libssh2_wincng_bignum_set_word(libssh2_bn *bn, ULONG word) +int ssh2_wcng_bn_set_word(ssh2_bn *bn, ULONG word) { ULONG offset, number, bits, length; @@ -312,7 +308,7 @@ int _libssh2_wincng_bignum_set_word(libssh2_bn *bn, ULONG word) bits++; length = (ULONG)(ceil(((double)bits) / 8.0) * sizeof(unsigned char)); - if(wincng_bignum_resize(bn, length)) + if(wcng_bn_resize(bn, length)) return -1; for(offset = 0; offset < length; offset++) @@ -321,7 +317,7 @@ int _libssh2_wincng_bignum_set_word(libssh2_bn *bn, ULONG word) return 0; } -ULONG _libssh2_wincng_bignum_bits(const libssh2_bn *bn) +ULONG ssh2_wcng_bn_bits(const ssh2_bn *bn) { unsigned char number; ULONG offset, length, bits; @@ -343,8 +339,7 @@ ULONG _libssh2_wincng_bignum_bits(const libssh2_bn *bn) return bits; } -int _libssh2_wincng_bignum_from_bin(libssh2_bn *bn, ULONG len, - const unsigned char *bin) +int ssh2_wcng_bn_from_bin(ssh2_bn *bn, ULONG len, const unsigned char *bin) { unsigned char *bignum; ULONG offset, length, bits; @@ -352,19 +347,19 @@ int _libssh2_wincng_bignum_from_bin(libssh2_bn *bn, ULONG len, if(!bn || !bin || !len) return -1; - if(wincng_bignum_resize(bn, len)) + if(wcng_bn_resize(bn, len)) return -1; memcpy(bn->bignum, bin, len); - bits = _libssh2_wincng_bignum_bits(bn); + bits = ssh2_wcng_bn_bits(bn); length = (ULONG)(ceil(((double)bits) / 8.0) * sizeof(unsigned char)); offset = bn->length - length; if(offset > 0) { memmove(bn->bignum, bn->bignum + offset, length); - _libssh2_explicit_zero(bn->bignum + length, offset); + ssh2_explicit_zero(bn->bignum + length, offset); bignum = realloc(bn->bignum, length); if(bignum) { @@ -379,7 +374,7 @@ int _libssh2_wincng_bignum_from_bin(libssh2_bn *bn, ULONG len, return 0; } -int _libssh2_wincng_bignum_to_bin(const libssh2_bn *bn, unsigned char *bin) +int ssh2_wcng_bn_to_bin(const ssh2_bn *bn, unsigned char *bin) { if(bin && bn && bn->bignum && bn->length > 0) { memcpy(bin, bn->bignum, bn->length); @@ -389,15 +384,15 @@ int _libssh2_wincng_bignum_to_bin(const libssh2_bn *bn, unsigned char *bin) return -1; } -void _libssh2_wincng_bignum_free(libssh2_bn *bn) +void ssh2_wcng_bn_free(ssh2_bn *bn) { if(bn) { if(bn->bignum) { - wincng_safe_free(bn->bignum, bn->length); + wcng_safe_free(bn->bignum, bn->length); bn->bignum = NULL; } bn->length = 0; - wincng_safe_free(bn, sizeof(libssh2_bn)); + wcng_safe_free(bn, sizeof(ssh2_bn)); } } @@ -408,9 +403,9 @@ void _libssh2_wincng_bignum_free(libssh2_bn *bn) #if LIBSSH2_ECDSA typedef enum { - WINCNG_ECC_KEYTYPE_ECDSA = 0, - WINCNG_ECC_KEYTYPE_ECDH = 1, -} wincng_ecc_keytype; + WCNG_ECC_KEYTYPE_ECDSA = 0, + WCNG_ECC_KEYTYPE_ECDH = 1, +} wcng_ecc_keytype; struct ecdsa_algorithm { /* Algorithm name */ @@ -423,18 +418,18 @@ struct ecdsa_algorithm { ULONG point_length; /* Name of CNG algorithm provider, */ - /* indexed by wincng_ecc_keytype */ + /* indexed by wcng_ecc_keytype */ LPCWSTR provider[2]; - /* Magic for public key import, indexed by wincng_ecc_keytype */ + /* Magic for public key import, indexed by wcng_ecc_keytype */ ULONG public_import_magic[2]; - /* Magic for private key import, indexed by wincng_ecc_keytype */ + /* Magic for private key import, indexed by wcng_ecc_keytype */ ULONG private_import_magic[2]; }; -/* Supported algorithms, indexed by libssh2_curve_type */ -static const struct ecdsa_algorithm wincng_ecdsa_algorithms[] = { +/* Supported algorithms, indexed by ssh2_curve_type */ +static const struct ecdsa_algorithm wcng_ecdsa_algs[] = { { "ecdsa-sha2-nistp256", 256, @@ -463,7 +458,7 @@ static const struct ecdsa_algorithm wincng_ecdsa_algorithms[] = { /* An encoded point */ struct ecdsa_point { - libssh2_curve_type curve; + ssh2_curve_type curve; const unsigned char *x; ULONG x_len; @@ -479,9 +474,9 @@ struct ecdsa_point { * Windows CNG backend: Generic functions */ -struct libssh2_wincng_ctx wincng_ctx; +struct wcng_ctx ssh2_wcng; -void _libssh2_wincng_init(void) +void ssh2_wcng_init(void) { int ret; @@ -489,234 +484,234 @@ void _libssh2_wincng_init(void) unsigned int curve; #endif - memset(&wincng_ctx, 0, sizeof(wincng_ctx)); + memset(&ssh2_wcng, 0, sizeof(ssh2_wcng)); - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgRNG, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgRNG, BCRYPT_RNG_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgRNG = NULL; + ssh2_wcng.hAlgRNG = NULL; } #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHashMD5, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHashMD5, BCRYPT_MD5_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHashMD5 = NULL; + ssh2_wcng.hAlgHashMD5 = NULL; } #endif - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHashSHA1, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHashSHA1, BCRYPT_SHA1_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHashSHA1 = NULL; + ssh2_wcng.hAlgHashSHA1 = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHashSHA256, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHashSHA256, BCRYPT_SHA256_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHashSHA256 = NULL; + ssh2_wcng.hAlgHashSHA256 = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHashSHA384, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHashSHA384, BCRYPT_SHA384_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHashSHA384 = NULL; + ssh2_wcng.hAlgHashSHA384 = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHashSHA512, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHashSHA512, BCRYPT_SHA512_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHashSHA512 = NULL; + ssh2_wcng.hAlgHashSHA512 = NULL; } #if LIBSSH2_MD5 - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHmacMD5, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHmacMD5, BCRYPT_MD5_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHmacMD5 = NULL; + ssh2_wcng.hAlgHmacMD5 = NULL; } #endif - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHmacSHA1, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHmacSHA1, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHmacSHA1 = NULL; + ssh2_wcng.hAlgHmacSHA1 = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHmacSHA256, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHmacSHA256, BCRYPT_SHA256_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHmacSHA256 = NULL; + ssh2_wcng.hAlgHmacSHA256 = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHmacSHA384, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHmacSHA384, BCRYPT_SHA384_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHmacSHA384 = NULL; + ssh2_wcng.hAlgHmacSHA384 = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgHmacSHA512, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgHmacSHA512, BCRYPT_SHA512_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgHmacSHA512 = NULL; + ssh2_wcng.hAlgHmacSHA512 = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgRSA, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgRSA, BCRYPT_RSA_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgRSA = NULL; + ssh2_wcng.hAlgRSA = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgDSA, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgDSA, BCRYPT_DSA_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgDSA = NULL; + ssh2_wcng.hAlgDSA = NULL; } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgAES_CBC, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgAES_CBC, BCRYPT_AES_ALGORITHM, NULL, 0); if(BCRYPT_SUCCESS(ret)) { - ret = BCryptSetProperty(wincng_ctx.hAlgAES_CBC, + ret = BCryptSetProperty(ssh2_wcng.hAlgAES_CBC, BCRYPT_CHAINING_MODE, - (PBYTE)LIBSSH2_UNCONST(BCRYPT_CHAIN_MODE_CBC), + (PBYTE)SSH2_UNCONST(BCRYPT_CHAIN_MODE_CBC), sizeof(BCRYPT_CHAIN_MODE_CBC), 0); if(!BCRYPT_SUCCESS(ret)) { - ret = BCryptCloseAlgorithmProvider(wincng_ctx.hAlgAES_CBC, 0); + ret = BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgAES_CBC, 0); if(BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgAES_CBC = NULL; + ssh2_wcng.hAlgAES_CBC = NULL; } } } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgAES_ECB, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgAES_ECB, BCRYPT_AES_ALGORITHM, NULL, 0); if(BCRYPT_SUCCESS(ret)) { - ret = BCryptSetProperty(wincng_ctx.hAlgAES_ECB, + ret = BCryptSetProperty(ssh2_wcng.hAlgAES_ECB, BCRYPT_CHAINING_MODE, - (PBYTE)LIBSSH2_UNCONST(BCRYPT_CHAIN_MODE_ECB), + (PBYTE)SSH2_UNCONST(BCRYPT_CHAIN_MODE_ECB), sizeof(BCRYPT_CHAIN_MODE_ECB), 0); if(!BCRYPT_SUCCESS(ret)) { - ret = BCryptCloseAlgorithmProvider(wincng_ctx.hAlgAES_ECB, 0); + ret = BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgAES_ECB, 0); if(BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgAES_ECB = NULL; + ssh2_wcng.hAlgAES_ECB = NULL; } } } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgRC4_NA, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgRC4_NA, BCRYPT_RC4_ALGORITHM, NULL, 0); if(BCRYPT_SUCCESS(ret)) { - ret = BCryptSetProperty(wincng_ctx.hAlgRC4_NA, + ret = BCryptSetProperty(ssh2_wcng.hAlgRC4_NA, BCRYPT_CHAINING_MODE, - (PBYTE)LIBSSH2_UNCONST(BCRYPT_CHAIN_MODE_NA), + (PBYTE)SSH2_UNCONST(BCRYPT_CHAIN_MODE_NA), sizeof(BCRYPT_CHAIN_MODE_NA), 0); if(!BCRYPT_SUCCESS(ret)) { - ret = BCryptCloseAlgorithmProvider(wincng_ctx.hAlgRC4_NA, 0); + ret = BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgRC4_NA, 0); if(BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgRC4_NA = NULL; + ssh2_wcng.hAlgRC4_NA = NULL; } } } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlg3DES_CBC, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlg3DES_CBC, BCRYPT_3DES_ALGORITHM, NULL, 0); if(BCRYPT_SUCCESS(ret)) { - ret = BCryptSetProperty(wincng_ctx.hAlg3DES_CBC, + ret = BCryptSetProperty(ssh2_wcng.hAlg3DES_CBC, BCRYPT_CHAINING_MODE, - (PBYTE)LIBSSH2_UNCONST(BCRYPT_CHAIN_MODE_CBC), + (PBYTE)SSH2_UNCONST(BCRYPT_CHAIN_MODE_CBC), sizeof(BCRYPT_CHAIN_MODE_CBC), 0); if(!BCRYPT_SUCCESS(ret)) { - ret = BCryptCloseAlgorithmProvider(wincng_ctx.hAlg3DES_CBC, 0); + ret = BCryptCloseAlgorithmProvider(ssh2_wcng.hAlg3DES_CBC, 0); if(BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlg3DES_CBC = NULL; + ssh2_wcng.hAlg3DES_CBC = NULL; } } } - ret = BCryptOpenAlgorithmProvider(&wincng_ctx.hAlgDH, + ret = BCryptOpenAlgorithmProvider(&ssh2_wcng.hAlgDH, BCRYPT_DH_ALGORITHM, NULL, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgDH = NULL; + ssh2_wcng.hAlgDH = NULL; } #if LIBSSH2_ECDSA - for(curve = 0; curve < ARRAY_SIZE(wincng_ecdsa_algorithms); curve++) { + for(curve = 0; curve < SSH2_ARRAYSIZE(wcng_ecdsa_algs); curve++) { BCRYPT_ALG_HANDLE alg_handle_ecdsa; BCRYPT_ALG_HANDLE alg_handle_ecdh; ret = BCryptOpenAlgorithmProvider( &alg_handle_ecdsa, - wincng_ecdsa_algorithms[curve].provider[WINCNG_ECC_KEYTYPE_ECDSA], + wcng_ecdsa_algs[curve].provider[WCNG_ECC_KEYTYPE_ECDSA], NULL, 0); if(BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgECDSA[curve] = alg_handle_ecdsa; + ssh2_wcng.hAlgECDSA[curve] = alg_handle_ecdsa; } ret = BCryptOpenAlgorithmProvider( &alg_handle_ecdh, - wincng_ecdsa_algorithms[curve].provider[WINCNG_ECC_KEYTYPE_ECDH], + wcng_ecdsa_algs[curve].provider[WCNG_ECC_KEYTYPE_ECDH], NULL, 0); if(BCRYPT_SUCCESS(ret)) { - wincng_ctx.hAlgECDH[curve] = alg_handle_ecdh; + ssh2_wcng.hAlgECDH[curve] = alg_handle_ecdh; } } #endif } -void _libssh2_wincng_free(void) +void ssh2_wcng_free(void) { #if LIBSSH2_ECDSA unsigned int curve; #endif - if(wincng_ctx.hAlgRNG) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgRNG, 0); + if(ssh2_wcng.hAlgRNG) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgRNG, 0); #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM - if(wincng_ctx.hAlgHashMD5) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHashMD5, 0); + if(ssh2_wcng.hAlgHashMD5) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHashMD5, 0); #endif - if(wincng_ctx.hAlgHashSHA1) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHashSHA1, 0); - if(wincng_ctx.hAlgHashSHA256) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHashSHA256, 0); - if(wincng_ctx.hAlgHashSHA384) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHashSHA384, 0); - if(wincng_ctx.hAlgHashSHA512) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHashSHA512, 0); + if(ssh2_wcng.hAlgHashSHA1) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHashSHA1, 0); + if(ssh2_wcng.hAlgHashSHA256) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHashSHA256, 0); + if(ssh2_wcng.hAlgHashSHA384) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHashSHA384, 0); + if(ssh2_wcng.hAlgHashSHA512) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHashSHA512, 0); #if LIBSSH2_MD5 - if(wincng_ctx.hAlgHmacMD5) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHmacMD5, 0); + if(ssh2_wcng.hAlgHmacMD5) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHmacMD5, 0); #endif - if(wincng_ctx.hAlgHmacSHA1) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHmacSHA1, 0); - if(wincng_ctx.hAlgHmacSHA256) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHmacSHA256, 0); - if(wincng_ctx.hAlgHmacSHA384) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHmacSHA384, 0); - if(wincng_ctx.hAlgHmacSHA512) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgHmacSHA512, 0); - if(wincng_ctx.hAlgRSA) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgRSA, 0); - if(wincng_ctx.hAlgDSA) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgDSA, 0); - if(wincng_ctx.hAlgAES_CBC) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgAES_CBC, 0); - if(wincng_ctx.hAlgRC4_NA) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgRC4_NA, 0); - if(wincng_ctx.hAlg3DES_CBC) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlg3DES_CBC, 0); - if(wincng_ctx.hAlgDH) - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgDH, 0); + if(ssh2_wcng.hAlgHmacSHA1) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHmacSHA1, 0); + if(ssh2_wcng.hAlgHmacSHA256) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHmacSHA256, 0); + if(ssh2_wcng.hAlgHmacSHA384) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHmacSHA384, 0); + if(ssh2_wcng.hAlgHmacSHA512) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgHmacSHA512, 0); + if(ssh2_wcng.hAlgRSA) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgRSA, 0); + if(ssh2_wcng.hAlgDSA) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgDSA, 0); + if(ssh2_wcng.hAlgAES_CBC) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgAES_CBC, 0); + if(ssh2_wcng.hAlgRC4_NA) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgRC4_NA, 0); + if(ssh2_wcng.hAlg3DES_CBC) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlg3DES_CBC, 0); + if(ssh2_wcng.hAlgDH) + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgDH, 0); #if LIBSSH2_ECDSA - for(curve = 0; curve < ARRAY_SIZE(wincng_ecdsa_algorithms); curve++) { - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgECDSA[curve], 0); - (void)BCryptCloseAlgorithmProvider(wincng_ctx.hAlgECDH[curve], 0); + for(curve = 0; curve < SSH2_ARRAYSIZE(wcng_ecdsa_algs); curve++) { + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgECDSA[curve], 0); + (void)BCryptCloseAlgorithmProvider(ssh2_wcng.hAlgECDH[curve], 0); } #endif - memset(&wincng_ctx, 0, sizeof(wincng_ctx)); + memset(&ssh2_wcng, 0, sizeof(ssh2_wcng)); } -int _libssh2_wincng_random(void *buf, size_t len) +int ssh2_wcng_random(void *buf, size_t len) { int ret; @@ -724,7 +719,7 @@ int _libssh2_wincng_random(void *buf, size_t len) return -1; } - ret = BCryptGenRandom(wincng_ctx.hAlgRNG, buf, (ULONG)len, 0); + ret = BCryptGenRandom(ssh2_wcng.hAlgRNG, buf, (ULONG)len, 0); return BCRYPT_SUCCESS(ret) ? 0 : -1; } @@ -734,9 +729,9 @@ int _libssh2_wincng_random(void *buf, size_t len) * Windows CNG backend: Hash functions */ -int _libssh2_wincng_hash_init(struct wincng_hash_ctx *ctx, - BCRYPT_ALG_HANDLE hAlg, ULONG hashlen, - unsigned char *key, ULONG keylen) +int ssh2_wcng_hash_init(struct wcng_hash_ctx *ctx, + BCRYPT_ALG_HANDLE hAlg, ULONG hashlen, + unsigned char *key, ULONG keylen) { BCRYPT_HASH_HANDLE hHash; unsigned char *pbHashObject; @@ -768,7 +763,7 @@ int _libssh2_wincng_hash_init(struct wincng_hash_ctx *ctx, pbHashObject, dwHashObject, key, keylen, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_safe_free(pbHashObject, dwHashObject); + wcng_safe_free(pbHashObject, dwHashObject); return -1; } @@ -780,19 +775,18 @@ int _libssh2_wincng_hash_init(struct wincng_hash_ctx *ctx, return 0; } -int _libssh2_wincng_hash_update(struct wincng_hash_ctx *ctx, - const void *data, ULONG datalen) +int ssh2_wcng_hash_update(struct wcng_hash_ctx *ctx, + const void *data, ULONG datalen) { int ret; ret = BCryptHashData(ctx->hHash, - (PUCHAR)LIBSSH2_UNCONST(data), datalen, 0); + (PUCHAR)SSH2_UNCONST(data), datalen, 0); return BCRYPT_SUCCESS(ret) ? 0 : -1; } -int _libssh2_wincng_hash_final(struct wincng_hash_ctx *ctx, - unsigned char *hash) +int ssh2_wcng_hash_final(struct wcng_hash_ctx *ctx, unsigned char *hash) { int ret; @@ -801,24 +795,24 @@ int _libssh2_wincng_hash_final(struct wincng_hash_ctx *ctx, BCryptDestroyHash(ctx->hHash); ctx->hHash = NULL; - wincng_safe_free(ctx->pbHashObject, ctx->dwHashObject); + wcng_safe_free(ctx->pbHashObject, ctx->dwHashObject); ctx->pbHashObject = NULL; ctx->dwHashObject = 0; return BCRYPT_SUCCESS(ret) ? 0 : -1; } -int _libssh2_wincng_hash(const unsigned char *data, ULONG datalen, - BCRYPT_ALG_HANDLE hAlg, - unsigned char *hash, ULONG hashlen) +int ssh2_wcng_hash(const unsigned char *data, ULONG datalen, + BCRYPT_ALG_HANDLE hAlg, + unsigned char *hash, ULONG hashlen) { - struct wincng_hash_ctx ctx; + struct wcng_hash_ctx ctx; int ret; - ret = _libssh2_wincng_hash_init(&ctx, hAlg, hashlen, NULL, 0); + ret = ssh2_wcng_hash_init(&ctx, hAlg, hashlen, NULL, 0); if(!ret) { - ret = _libssh2_wincng_hash_update(&ctx, data, datalen); - ret |= _libssh2_wincng_hash_final(&ctx, hash); + ret = ssh2_wcng_hash_update(&ctx, data, datalen); + ret |= ssh2_wcng_hash_final(&ctx, hash); } return ret; @@ -829,75 +823,66 @@ int _libssh2_wincng_hash(const unsigned char *data, ULONG datalen, * Windows CNG backend: HMAC functions */ -int _libssh2_hmac_ctx_init(libssh2_hmac_ctx *ctx) +int ssh2_hmac_ctx_init(ssh2_hmac_ctx *ctx) { memset(ctx, 0, sizeof(*ctx)); return 1; } #if LIBSSH2_MD5 -int _libssh2_hmac_md5_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen) +int ssh2_hmac_md5_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - int ret = _libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHmacMD5, - MD5_DIGEST_LENGTH, - key, (ULONG)keylen); + int ret = ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHmacMD5, + MD5_DIGEST_LENGTH, key, (ULONG)keylen); return ret == 0 ? 1 : 0; } #endif -int _libssh2_hmac_sha1_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen) +int ssh2_hmac_sha1_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - int ret = _libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHmacSHA1, - SHA_DIGEST_LENGTH, - key, (ULONG)keylen); + int ret = ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHmacSHA1, + SHA_DIGEST_LENGTH, key, (ULONG)keylen); return ret == 0 ? 1 : 0; } -int _libssh2_hmac_sha256_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen) +int ssh2_hmac_sha256_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - int ret = _libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHmacSHA256, - SHA256_DIGEST_LENGTH, - key, (ULONG)keylen); + int ret = ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHmacSHA256, + SHA256_DIGEST_LENGTH, key, (ULONG)keylen); return ret == 0 ? 1 : 0; } -int _libssh2_hmac_sha512_init(libssh2_hmac_ctx *ctx, - void *key, size_t keylen) +int ssh2_hmac_sha512_init(ssh2_hmac_ctx *ctx, void *key, size_t keylen) { - int ret = _libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHmacSHA512, - SHA512_DIGEST_LENGTH, - key, (ULONG)keylen); + int ret = ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHmacSHA512, + SHA512_DIGEST_LENGTH, key, (ULONG)keylen); return ret == 0 ? 1 : 0; } -int _libssh2_hmac_update(libssh2_hmac_ctx *ctx, - const void *data, size_t datalen) +int ssh2_hmac_update(ssh2_hmac_ctx *ctx, const void *data, size_t datalen) { - int ret = _libssh2_wincng_hash_update(ctx, data, (ULONG)datalen); + int ret = ssh2_wcng_hash_update(ctx, data, (ULONG)datalen); return ret == 0 ? 1 : 0; } -int _libssh2_hmac_final(libssh2_hmac_ctx *ctx, void *data) +int ssh2_hmac_final(ssh2_hmac_ctx *ctx, void *data) { int ret = BCryptFinishHash(ctx->hHash, data, ctx->cbHash, 0); return BCRYPT_SUCCESS(ret) ? 1 : 0; } -void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) +void ssh2_hmac_cleanup(ssh2_hmac_ctx *ctx) { BCryptDestroyHash(ctx->hHash); ctx->hHash = NULL; - wincng_safe_free(ctx->pbHashObject, ctx->dwHashObject); + wcng_safe_free(ctx->pbHashObject, ctx->dwHashObject); ctx->pbHashObject = NULL; ctx->dwHashObject = 0; } @@ -907,13 +892,13 @@ void _libssh2_hmac_cleanup(libssh2_hmac_ctx *ctx) * Windows CNG backend: Key functions */ -static int wincng_key_sha_verify(struct wincng_key_ctx *ctx, - ULONG hashlen, - const unsigned char *sig, - ULONG sig_len, - const unsigned char *m, - ULONG m_len, - ULONG flags) +static int wcng_key_sha_verify(struct wcng_key_ctx *ctx, + ULONG hashlen, + const unsigned char *sig, + ULONG sig_len, + const unsigned char *m, + ULONG m_len, + ULONG flags) { BCRYPT_PKCS1_PADDING_INFO paddingInfoPKCS1; BCRYPT_ALG_HANDLE hAlgHash; @@ -923,19 +908,19 @@ static int wincng_key_sha_verify(struct wincng_key_ctx *ctx, int ret; if(hashlen == SHA_DIGEST_LENGTH) { - hAlgHash = wincng_ctx.hAlgHashSHA1; + hAlgHash = ssh2_wcng.hAlgHashSHA1; paddingInfoPKCS1.pszAlgId = BCRYPT_SHA1_ALGORITHM; } else if(hashlen == SHA256_DIGEST_LENGTH) { - hAlgHash = wincng_ctx.hAlgHashSHA256; + hAlgHash = ssh2_wcng.hAlgHashSHA256; paddingInfoPKCS1.pszAlgId = BCRYPT_SHA256_ALGORITHM; } else if(hashlen == SHA384_DIGEST_LENGTH) { - hAlgHash = wincng_ctx.hAlgHashSHA384; + hAlgHash = ssh2_wcng.hAlgHashSHA384; paddingInfoPKCS1.pszAlgId = BCRYPT_SHA384_ALGORITHM; } else if(hashlen == SHA512_DIGEST_LENGTH) { - hAlgHash = wincng_ctx.hAlgHashSHA512; + hAlgHash = ssh2_wcng.hAlgHashSHA512; paddingInfoPKCS1.pszAlgId = BCRYPT_SHA512_ALGORITHM; } else { @@ -955,20 +940,18 @@ static int wincng_key_sha_verify(struct wincng_key_ctx *ctx, } memcpy(data, m, datalen); - ret = _libssh2_wincng_hash(data, datalen, - hAlgHash, - hash, hashlen); - wincng_safe_free(data, datalen); + ret = ssh2_wcng_hash(data, datalen, hAlgHash, hash, hashlen); + wcng_safe_free(data, datalen); if(ret) { - wincng_safe_free(hash, hashlen); + wcng_safe_free(hash, hashlen); return -1; } datalen = sig_len; data = malloc(datalen); if(!data) { - wincng_safe_free(hash, hashlen); + wcng_safe_free(hash, hashlen); return -1; } @@ -983,20 +966,20 @@ static int wincng_key_sha_verify(struct wincng_key_ctx *ctx, ret = BCryptVerifySignature(ctx->hKey, pPaddingInfo, hash, hashlen, data, datalen, flags); - wincng_safe_free(hash, hashlen); - wincng_safe_free(data, datalen); + wcng_safe_free(hash, hashlen); + wcng_safe_free(data, datalen); return BCRYPT_SUCCESS(ret) ? 0 : -1; } #ifdef HAVE_LIBCRYPT32 -static int wincng_load_pem(LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase, - const char *headerbegin, - const char *headerend, - unsigned char **data, - size_t *datalen) +static int wcng_load_pem(LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase, + const char *headerbegin, + const char *headerend, + unsigned char **data, + size_t *datalen) { FILE *fp; int ret; @@ -1006,36 +989,36 @@ static int wincng_load_pem(LIBSSH2_SESSION *session, return -1; } - ret = _libssh2_pem_parse(session, headerbegin, headerend, - passphrase, - fp, data, datalen); + ret = ssh2_pem_parse(session, headerbegin, headerend, + passphrase, + fp, data, datalen); fclose(fp); return ret; } -static int wincng_load_private(LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase, - unsigned char **ppbEncoded, - size_t *pcbEncoded, - int tryLoadRSA, int tryLoadDSA) +static int wcng_load_private(LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase, + unsigned char **ppbEncoded, + size_t *pcbEncoded, + int tryLoadRSA, int tryLoadDSA) { unsigned char *data = NULL; size_t datalen = 0; int ret = -1; if(ret && tryLoadRSA) { - ret = wincng_load_pem(session, filename, passphrase, - PEM_RSA_HEADER, PEM_RSA_FOOTER, - &data, &datalen); + ret = wcng_load_pem(session, filename, passphrase, + PEM_RSA_HEADER, PEM_RSA_FOOTER, + &data, &datalen); } if(ret && tryLoadDSA) { - ret = wincng_load_pem(session, filename, passphrase, - PEM_DSA_HEADER, PEM_DSA_FOOTER, - &data, &datalen); + ret = wcng_load_pem(session, filename, passphrase, + PEM_DSA_HEADER, PEM_DSA_FOOTER, + &data, &datalen); } if(!ret) { @@ -1046,32 +1029,32 @@ static int wincng_load_private(LIBSSH2_SESSION *session, return ret; } -static int wincng_load_private_memory(LIBSSH2_SESSION *session, - const char *privatekeydata, - size_t privatekeydata_len, - const unsigned char *passphrase, - unsigned char **ppbEncoded, - size_t *pcbEncoded, - int tryLoadRSA, int tryLoadDSA) +static int wcng_load_private_memory(LIBSSH2_SESSION *session, + const char *privatekeydata, + size_t privatekeydata_len, + const unsigned char *passphrase, + unsigned char **ppbEncoded, + size_t *pcbEncoded, + int tryLoadRSA, int tryLoadDSA) { unsigned char *data = NULL; size_t datalen = 0; int ret = -1; if(ret && tryLoadRSA) { - ret = _libssh2_pem_parse_memory(session, - PEM_RSA_HEADER, PEM_RSA_FOOTER, - passphrase, - privatekeydata, privatekeydata_len, - &data, &datalen); + ret = ssh2_pem_parse_memory(session, + PEM_RSA_HEADER, PEM_RSA_FOOTER, + passphrase, + privatekeydata, privatekeydata_len, + &data, &datalen); } if(ret && tryLoadDSA) { - ret = _libssh2_pem_parse_memory(session, - PEM_DSA_HEADER, PEM_DSA_FOOTER, - passphrase, - privatekeydata, privatekeydata_len, - &data, &datalen); + ret = ssh2_pem_parse_memory(session, + PEM_DSA_HEADER, PEM_DSA_FOOTER, + passphrase, + privatekeydata, privatekeydata_len, + &data, &datalen); } if(!ret) { @@ -1082,11 +1065,9 @@ static int wincng_load_private_memory(LIBSSH2_SESSION *session, return ret; } -static int wincng_asn_decode(unsigned char *pbEncoded, - DWORD cbEncoded, - LPCSTR lpszStructType, - unsigned char **ppbDecoded, - DWORD *pcbDecoded) +static int wcng_asn_decode(unsigned char *pbEncoded, DWORD cbEncoded, + LPCSTR lpszStructType, + unsigned char **ppbDecoded, DWORD *pcbDecoded) { unsigned char *pbDecoded = NULL; DWORD cbDecoded = 0; @@ -1110,7 +1091,7 @@ static int wincng_asn_decode(unsigned char *pbEncoded, pbEncoded, cbEncoded, 0, NULL, pbDecoded, &cbDecoded); if(!ret) { - wincng_safe_free(pbDecoded, cbDecoded); + wcng_safe_free(pbDecoded, cbDecoded); return -1; } @@ -1120,10 +1101,10 @@ static int wincng_asn_decode(unsigned char *pbEncoded, return 0; } -static int wincng_bn_ltob(unsigned char *pbInput, - DWORD cbInput, - unsigned char **ppbOutput, - DWORD *pcbOutput) +static int wcng_bn_ltob(unsigned char *pbInput, + DWORD cbInput, + unsigned char **ppbOutput, + DWORD *pcbOutput) { unsigned char *pbOutput; DWORD cbOutput, index, offset, length; @@ -1157,38 +1138,35 @@ static int wincng_bn_ltob(unsigned char *pbInput, return 0; } -static int wincng_asn_decode_bn(unsigned char *pbEncoded, - DWORD cbEncoded, - unsigned char **ppbDecoded, - DWORD *pcbDecoded) +static int wcng_asn_decode_bn(unsigned char *pbEncoded, DWORD cbEncoded, + unsigned char **ppbDecoded, DWORD *pcbDecoded) { unsigned char *pbDecoded = NULL; PCRYPT_DATA_BLOB pbInteger; DWORD cbDecoded = 0, cbInteger; int ret; - ret = wincng_asn_decode(pbEncoded, cbEncoded, - X509_MULTI_BYTE_UINT, - (void *)&pbInteger, &cbInteger); + ret = wcng_asn_decode(pbEncoded, cbEncoded, X509_MULTI_BYTE_UINT, + (void *)&pbInteger, &cbInteger); if(!ret) { - ret = wincng_bn_ltob(pbInteger->pbData, - pbInteger->cbData, - &pbDecoded, &cbDecoded); + ret = wcng_bn_ltob(pbInteger->pbData, + pbInteger->cbData, + &pbDecoded, &cbDecoded); if(!ret) { *ppbDecoded = pbDecoded; *pcbDecoded = cbDecoded; } - wincng_safe_free(pbInteger, cbInteger); + wcng_safe_free(pbInteger, cbInteger); } return ret; } -static int wincng_asn_decode_bns(unsigned char *pbEncoded, - DWORD cbEncoded, - unsigned char ***prpbDecoded, - DWORD **prcbDecoded, - DWORD *pcbCount) +static int wcng_asn_decode_bns(unsigned char *pbEncoded, + DWORD cbEncoded, + unsigned char ***prpbDecoded, + DWORD **prcbDecoded, + DWORD *pcbCount) { PCRYPT_DER_BLOB pBlob; unsigned char **rpbDecoded; @@ -1196,9 +1174,8 @@ static int wincng_asn_decode_bns(unsigned char *pbEncoded, DWORD cbDecoded, *rcbDecoded, index, length; int ret; - ret = wincng_asn_decode(pbEncoded, cbEncoded, - X509_SEQUENCE_OF_ANY, - (void *)&pbDecoded, &cbDecoded); + ret = wcng_asn_decode(pbEncoded, cbEncoded, X509_SEQUENCE_OF_ANY, + (void *)&pbDecoded, &cbDecoded); if(!ret) { length = pbDecoded->cValue; @@ -1208,10 +1185,10 @@ static int wincng_asn_decode_bns(unsigned char *pbEncoded, if(rcbDecoded) { for(index = 0; index < length; index++) { pBlob = &pbDecoded->rgValue[index]; - ret = wincng_asn_decode_bn(pBlob->pbData, - pBlob->cbData, - &rpbDecoded[index], - &rcbDecoded[index]); + ret = wcng_asn_decode_bn(pBlob->pbData, + pBlob->cbData, + &rpbDecoded[index], + &rcbDecoded[index]); if(ret) break; } @@ -1223,8 +1200,8 @@ static int wincng_asn_decode_bns(unsigned char *pbEncoded, } else { for(length = 0; length < index; length++) { - wincng_safe_free(rpbDecoded[length], - rcbDecoded[length]); + wcng_safe_free(rpbDecoded[length], + rcbDecoded[length]); rpbDecoded[length] = NULL; rcbDecoded[length] = 0; } @@ -1241,7 +1218,7 @@ static int wincng_asn_decode_bns(unsigned char *pbEncoded, ret = -1; } - wincng_safe_free(pbDecoded, cbDecoded); + wcng_safe_free(pbDecoded, cbDecoded); } return ret; @@ -1249,7 +1226,7 @@ static int wincng_asn_decode_bns(unsigned char *pbEncoded, #endif /* HAVE_LIBCRYPT32 */ #if LIBSSH2_RSA || LIBSSH2_DSA -static ULONG wincng_bn_size(const unsigned char *bignum, ULONG length) +static ULONG wcng_bn_size(const unsigned char *bignum, ULONG length) { ULONG offset; @@ -1274,16 +1251,16 @@ static ULONG wincng_bn_size(const unsigned char *bignum, ULONG length) * Windows CNG backend: RSA functions */ -int _libssh2_wincng_rsa_new(libssh2_rsa_ctx **rsa, - const unsigned char *edata, unsigned long elen, - const unsigned char *ndata, unsigned long nlen, - const unsigned char *ddata, unsigned long dlen, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *e1data, unsigned long e1len, - const unsigned char *e2data, unsigned long e2len, - const unsigned char *coeffdata, - unsigned long coefflen) +int ssh2_wcng_rsa_new(ssh2_rsa_ctx **rsa, + const unsigned char *edata, unsigned long elen, + const unsigned char *ndata, unsigned long nlen, + const unsigned char *ddata, unsigned long dlen, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *e1data, unsigned long e1len, + const unsigned char *e2data, unsigned long e2len, + const unsigned char *coeffdata, + unsigned long coefflen) { BCRYPT_KEY_HANDLE hKey; BCRYPT_RSAKEY_BLOB *rsakey; @@ -1291,15 +1268,15 @@ int _libssh2_wincng_rsa_new(libssh2_rsa_ctx **rsa, ULONG keylen, offset, mlen, p1len = 0, p2len = 0; int ret; - mlen = max(wincng_bn_size(ndata, nlen), - wincng_bn_size(ddata, dlen)); + mlen = max(wcng_bn_size(ndata, nlen), + wcng_bn_size(ddata, dlen)); offset = sizeof(BCRYPT_RSAKEY_BLOB); keylen = offset + elen + mlen; if(ddata && dlen > 0) { - p1len = max(wincng_bn_size(pdata, plen), - wincng_bn_size(e1data, e1len)); - p2len = max(wincng_bn_size(qdata, qlen), - wincng_bn_size(e2data, e2len)); + p1len = max(wcng_bn_size(pdata, plen), + wcng_bn_size(e1data, e1len)); + p2len = max(wcng_bn_size(qdata, qlen), + wcng_bn_size(e2data, e2len)); keylen += p1len * 3 + p2len * 2 + mlen; } @@ -1385,17 +1362,17 @@ int _libssh2_wincng_rsa_new(libssh2_rsa_ctx **rsa, rsakey->cbPrime2 = 0; } - ret = BCryptImportKeyPair(wincng_ctx.hAlgRSA, NULL, lpszBlobType, + ret = BCryptImportKeyPair(ssh2_wcng.hAlgRSA, NULL, lpszBlobType, &hKey, (PUCHAR)rsakey, keylen, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_safe_free(rsakey, keylen); + wcng_safe_free(rsakey, keylen); return -1; } - *rsa = malloc(sizeof(libssh2_rsa_ctx)); + *rsa = malloc(sizeof(ssh2_rsa_ctx)); if(!(*rsa)) { BCryptDestroyKey(hKey); - wincng_safe_free(rsakey, keylen); + wcng_safe_free(rsakey, keylen); return -1; } @@ -1407,10 +1384,10 @@ int _libssh2_wincng_rsa_new(libssh2_rsa_ctx **rsa, } #ifdef HAVE_LIBCRYPT32 -static int wincng_rsa_new_private_parse(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - unsigned char *pbEncoded, - size_t cbEncoded) +static int wcng_rsa_new_private_parse(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + unsigned char *pbEncoded, + size_t cbEncoded) { BCRYPT_KEY_HANDLE hKey; unsigned char *pbStructInfo; @@ -1419,28 +1396,26 @@ static int wincng_rsa_new_private_parse(libssh2_rsa_ctx **rsa, (void)session; - ret = wincng_asn_decode(pbEncoded, (DWORD)cbEncoded, - PKCS_RSA_PRIVATE_KEY, - &pbStructInfo, &cbStructInfo); + ret = wcng_asn_decode(pbEncoded, (DWORD)cbEncoded, PKCS_RSA_PRIVATE_KEY, + &pbStructInfo, &cbStructInfo); - wincng_safe_free(pbEncoded, cbEncoded); + wcng_safe_free(pbEncoded, cbEncoded); if(ret) { return -1; } - ret = BCryptImportKeyPair(wincng_ctx.hAlgRSA, NULL, - LEGACY_RSAPRIVATE_BLOB, &hKey, - pbStructInfo, cbStructInfo, 0); + ret = BCryptImportKeyPair(ssh2_wcng.hAlgRSA, NULL, LEGACY_RSAPRIVATE_BLOB, + &hKey, pbStructInfo, cbStructInfo, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_safe_free(pbStructInfo, cbStructInfo); + wcng_safe_free(pbStructInfo, cbStructInfo); return -1; } - *rsa = malloc(sizeof(libssh2_rsa_ctx)); + *rsa = malloc(sizeof(ssh2_rsa_ctx)); if(!(*rsa)) { BCryptDestroyKey(hKey); - wincng_safe_free(pbStructInfo, cbStructInfo); + wcng_safe_free(pbStructInfo, cbStructInfo); return -1; } @@ -1452,100 +1427,99 @@ static int wincng_rsa_new_private_parse(libssh2_rsa_ctx **rsa, } #endif /* HAVE_LIBCRYPT32 */ -int _libssh2_wincng_rsa_new_private(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_wcng_rsa_new_private(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { #ifdef HAVE_LIBCRYPT32 unsigned char *pbEncoded; size_t cbEncoded; int ret; - ret = wincng_load_private(session, filename, passphrase, - &pbEncoded, &cbEncoded, 1, 0); + ret = wcng_load_private(session, filename, passphrase, + &pbEncoded, &cbEncoded, 1, 0); if(ret) { return -1; } - return wincng_rsa_new_private_parse(rsa, session, pbEncoded, cbEncoded); + return wcng_rsa_new_private_parse(rsa, session, pbEncoded, cbEncoded); #else (void)rsa; (void)filename; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to load RSA key from private key file: " - "Method unsupported in Windows CNG backend"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to load RSA key from private key file: " + "Method unsupported in Windows CNG backend"); #endif /* HAVE_LIBCRYPT32 */ } -int _libssh2_wincng_rsa_new_private_frommemory(libssh2_rsa_ctx **rsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_wcng_rsa_new_private_frommemory(ssh2_rsa_ctx **rsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { #ifdef HAVE_LIBCRYPT32 unsigned char *pbEncoded; size_t cbEncoded; int ret; - ret = wincng_load_private_memory(session, filedata, filedata_len, - passphrase, - &pbEncoded, &cbEncoded, 1, 0); + ret = wcng_load_private_memory(session, filedata, filedata_len, + passphrase, &pbEncoded, &cbEncoded, 1, 0); if(ret) { return -1; } - return wincng_rsa_new_private_parse(rsa, session, pbEncoded, cbEncoded); + return wcng_rsa_new_private_parse(rsa, session, pbEncoded, cbEncoded); #else (void)rsa; (void)filedata; (void)filedata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unable to extract private key from memory: " - "Method unsupported in Windows CNG backend"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unable to extract private key from memory: " + "Method unsupported in Windows CNG backend"); #endif /* HAVE_LIBCRYPT32 */ } #if LIBSSH2_RSA_SHA1 -int _libssh2_wincng_rsa_sha1_verify(libssh2_rsa_ctx *rsactx, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, - size_t m_len) +int ssh2_wcng_rsa_sha1_verify(ssh2_rsa_ctx *rsactx, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, + size_t m_len) { - return wincng_key_sha_verify(rsactx, SHA_DIGEST_LENGTH, - sig, (ULONG)sig_len, - m, (ULONG)m_len, - BCRYPT_PAD_PKCS1); + return wcng_key_sha_verify(rsactx, SHA_DIGEST_LENGTH, + sig, (ULONG)sig_len, + m, (ULONG)m_len, + BCRYPT_PAD_PKCS1); } #endif #if LIBSSH2_RSA_SHA2 -int _libssh2_wincng_rsa_sha2_verify(libssh2_rsa_ctx *rsactx, - size_t hash_len, - const unsigned char *sig, - size_t sig_len, - const unsigned char *m, - size_t m_len) +int ssh2_wcng_rsa_sha2_verify(ssh2_rsa_ctx *rsactx, + size_t hash_len, + const unsigned char *sig, + size_t sig_len, + const unsigned char *m, + size_t m_len) { - return wincng_key_sha_verify(rsactx, (ULONG)hash_len, - sig, (ULONG)sig_len, - m, (ULONG)m_len, - BCRYPT_PAD_PKCS1); + return wcng_key_sha_verify(rsactx, (ULONG)hash_len, + sig, (ULONG)sig_len, + m, (ULONG)m_len, + BCRYPT_PAD_PKCS1); } #endif -static int wincng_rsa_sha_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsa, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +static int wcng_rsa_sha_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsa, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { BCRYPT_PKCS1_PADDING_INFO paddingInfo; unsigned char *data, *sig; @@ -1561,8 +1535,8 @@ static int wincng_rsa_sha_sign(LIBSSH2_SESSION *session, else if(hash_len == SHA512_DIGEST_LENGTH) paddingInfo.pszAlgId = BCRYPT_SHA512_ALGORITHM; else { - _libssh2_error(session, LIBSSH2_ERROR_PROTO, - "Unsupported hash digest length"); + ssh2_err(session, LIBSSH2_ERROR_PROTO, + "Unsupported hash digest length"); return -1; } @@ -1578,7 +1552,7 @@ static int wincng_rsa_sha_sign(LIBSSH2_SESSION *session, &cbData, BCRYPT_PAD_PKCS1); if(BCRYPT_SUCCESS(ret)) { siglen = cbData; - sig = LIBSSH2_ALLOC(session, siglen); + sig = SSH2_ALLOC(session, siglen); if(sig) { ret = BCryptSignHash(rsa->hKey, &paddingInfo, data, datalen, sig, siglen, @@ -1588,43 +1562,43 @@ static int wincng_rsa_sha_sign(LIBSSH2_SESSION *session, *signature = sig; } else { - LIBSSH2_FREE(session, sig); + SSH2_FREE(session, sig); } } else ret = (NTSTATUS)STATUS_NO_MEMORY; } - wincng_safe_free(data, datalen); + wcng_safe_free(data, datalen); return BCRYPT_SUCCESS(ret) ? 0 : -1; } -int _libssh2_wincng_rsa_sha1_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +int ssh2_wcng_rsa_sha1_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { - return wincng_rsa_sha_sign(session, rsactx, - hash, hash_len, - signature, signature_len); + return wcng_rsa_sha_sign(session, rsactx, + hash, hash_len, + signature, signature_len); } -int _libssh2_wincng_rsa_sha2_sign(LIBSSH2_SESSION *session, - libssh2_rsa_ctx *rsactx, - const unsigned char *hash, - size_t hash_len, - unsigned char **signature, - size_t *signature_len) +int ssh2_wcng_rsa_sha2_sign(LIBSSH2_SESSION *session, + ssh2_rsa_ctx *rsactx, + const unsigned char *hash, + size_t hash_len, + unsigned char **signature, + size_t *signature_len) { - return wincng_rsa_sha_sign(session, rsactx, - hash, hash_len, - signature, signature_len); + return wcng_rsa_sha_sign(session, rsactx, + hash, hash_len, + signature, signature_len); } -void _libssh2_wincng_rsa_free(libssh2_rsa_ctx *rsa) +void ssh2_wcng_rsa_free(ssh2_rsa_ctx *rsa) { if(!rsa) return; @@ -1632,8 +1606,8 @@ void _libssh2_wincng_rsa_free(libssh2_rsa_ctx *rsa) BCryptDestroyKey(rsa->hKey); rsa->hKey = NULL; - wincng_safe_free(rsa->pbKeyObject, rsa->cbKeyObject); - wincng_safe_free(rsa, sizeof(libssh2_rsa_ctx)); + wcng_safe_free(rsa->pbKeyObject, rsa->cbKeyObject); + wcng_safe_free(rsa, sizeof(ssh2_rsa_ctx)); } #endif @@ -1643,12 +1617,12 @@ void _libssh2_wincng_rsa_free(libssh2_rsa_ctx *rsa) */ #if LIBSSH2_DSA -int _libssh2_wincng_dsa_new(libssh2_dsa_ctx **dsa, - const unsigned char *pdata, unsigned long plen, - const unsigned char *qdata, unsigned long qlen, - const unsigned char *gdata, unsigned long glen, - const unsigned char *ydata, unsigned long ylen, - const unsigned char *xdata, unsigned long xlen) +int ssh2_wcng_dsa_new(ssh2_dsa_ctx **dsa, + const unsigned char *pdata, unsigned long plen, + const unsigned char *qdata, unsigned long qlen, + const unsigned char *gdata, unsigned long glen, + const unsigned char *ydata, unsigned long ylen, + const unsigned char *xdata, unsigned long xlen) { BCRYPT_KEY_HANDLE hKey; BCRYPT_DSA_KEY_BLOB *dsakey; @@ -1656,9 +1630,9 @@ int _libssh2_wincng_dsa_new(libssh2_dsa_ctx **dsa, ULONG keylen, offset, length; int ret; - length = max(max(wincng_bn_size(pdata, plen), - wincng_bn_size(gdata, glen)), - wincng_bn_size(ydata, ylen)); + length = max(max(wcng_bn_size(pdata, plen), + wcng_bn_size(gdata, glen)), + wcng_bn_size(ydata, ylen)); offset = sizeof(BCRYPT_DSA_KEY_BLOB); keylen = offset + length * 3; if(xdata && xlen > 0) @@ -1721,17 +1695,17 @@ int _libssh2_wincng_dsa_new(libssh2_dsa_ctx **dsa, dsakey->dwMagic = BCRYPT_DSA_PUBLIC_MAGIC; } - ret = BCryptImportKeyPair(wincng_ctx.hAlgDSA, NULL, lpszBlobType, + ret = BCryptImportKeyPair(ssh2_wcng.hAlgDSA, NULL, lpszBlobType, &hKey, (PUCHAR)dsakey, keylen, 0); if(!BCRYPT_SUCCESS(ret)) { - wincng_safe_free(dsakey, keylen); + wcng_safe_free(dsakey, keylen); return -1; } - *dsa = malloc(sizeof(libssh2_dsa_ctx)); + *dsa = malloc(sizeof(ssh2_dsa_ctx)); if(!(*dsa)) { BCryptDestroyKey(hKey); - wincng_safe_free(dsakey, keylen); + wcng_safe_free(dsakey, keylen); return -1; } @@ -1743,10 +1717,10 @@ int _libssh2_wincng_dsa_new(libssh2_dsa_ctx **dsa, } #ifdef HAVE_LIBCRYPT32 -static int wincng_dsa_new_private_parse(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - unsigned char *pbEncoded, - size_t cbEncoded) +static int wcng_dsa_new_private_parse(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + unsigned char *pbEncoded, + size_t cbEncoded) { unsigned char **rpbDecoded; DWORD *rcbDecoded, index, length; @@ -1754,29 +1728,29 @@ static int wincng_dsa_new_private_parse(libssh2_dsa_ctx **dsa, (void)session; - ret = wincng_asn_decode_bns(pbEncoded, (DWORD)cbEncoded, - &rpbDecoded, &rcbDecoded, &length); + ret = wcng_asn_decode_bns(pbEncoded, (DWORD)cbEncoded, + &rpbDecoded, &rcbDecoded, &length); - wincng_safe_free(pbEncoded, cbEncoded); + wcng_safe_free(pbEncoded, cbEncoded); if(ret) { return -1; } if(length == 6) { - ret = _libssh2_wincng_dsa_new(dsa, - rpbDecoded[1], rcbDecoded[1], - rpbDecoded[2], rcbDecoded[2], - rpbDecoded[3], rcbDecoded[3], - rpbDecoded[4], rcbDecoded[4], - rpbDecoded[5], rcbDecoded[5]); + ret = ssh2_wcng_dsa_new(dsa, + rpbDecoded[1], rcbDecoded[1], + rpbDecoded[2], rcbDecoded[2], + rpbDecoded[3], rcbDecoded[3], + rpbDecoded[4], rcbDecoded[4], + rpbDecoded[5], rcbDecoded[5]); } else { ret = -1; } for(index = 0; index < length; index++) { - wincng_safe_free(rpbDecoded[index], rcbDecoded[index]); + wcng_safe_free(rpbDecoded[index], rcbDecoded[index]); rpbDecoded[index] = NULL; rcbDecoded[index] = 0; } @@ -1788,77 +1762,77 @@ static int wincng_dsa_new_private_parse(libssh2_dsa_ctx **dsa, } #endif /* HAVE_LIBCRYPT32 */ -int _libssh2_wincng_dsa_new_private(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filename, - const unsigned char *passphrase) +int ssh2_wcng_dsa_new_private(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filename, + const unsigned char *passphrase) { #ifdef HAVE_LIBCRYPT32 unsigned char *pbEncoded; size_t cbEncoded; int ret; - ret = wincng_load_private(session, filename, passphrase, - &pbEncoded, &cbEncoded, 0, 1); + ret = wcng_load_private(session, filename, passphrase, + &pbEncoded, &cbEncoded, 0, 1); if(ret) { return -1; } - return wincng_dsa_new_private_parse(dsa, session, pbEncoded, cbEncoded); + return wcng_dsa_new_private_parse(dsa, session, pbEncoded, cbEncoded); #else (void)dsa; (void)filename; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to load DSA key from private key file: " - "Method unsupported in Windows CNG backend"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to load DSA key from private key file: " + "Method unsupported in Windows CNG backend"); #endif /* HAVE_LIBCRYPT32 */ } -int _libssh2_wincng_dsa_new_private_frommemory(libssh2_dsa_ctx **dsa, - LIBSSH2_SESSION *session, - const char *filedata, - size_t filedata_len, - const unsigned char *passphrase) +int ssh2_wcng_dsa_new_private_frommemory(ssh2_dsa_ctx **dsa, + LIBSSH2_SESSION *session, + const char *filedata, + size_t filedata_len, + const unsigned char *passphrase) { #ifdef HAVE_LIBCRYPT32 unsigned char *pbEncoded; size_t cbEncoded; int ret; - ret = wincng_load_private_memory(session, filedata, filedata_len, - passphrase, &pbEncoded, &cbEncoded, 0, 1); + ret = wcng_load_private_memory(session, filedata, filedata_len, + passphrase, &pbEncoded, &cbEncoded, 0, 1); if(ret) { return -1; } - return wincng_dsa_new_private_parse(dsa, session, pbEncoded, cbEncoded); + return wcng_dsa_new_private_parse(dsa, session, pbEncoded, cbEncoded); #else (void)dsa; (void)filedata; (void)filedata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unable to extract private key from memory: " - "Method unsupported in Windows CNG backend"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unable to extract private key from memory: " + "Method unsupported in Windows CNG backend"); #endif /* HAVE_LIBCRYPT32 */ } -int _libssh2_wincng_dsa_sha1_verify(libssh2_dsa_ctx *dsa, - const unsigned char *sig_fixed, - const unsigned char *m, - size_t m_len) +int ssh2_wcng_dsa_sha1_verify(ssh2_dsa_ctx *dsa, + const unsigned char *sig_fixed, + const unsigned char *m, + size_t m_len) { - return wincng_key_sha_verify(dsa, SHA_DIGEST_LENGTH, sig_fixed, - 40, m, (ULONG)m_len, 0); + return wcng_key_sha_verify(dsa, SHA_DIGEST_LENGTH, sig_fixed, + 40, m, (ULONG)m_len, 0); } -int _libssh2_wincng_dsa_sha1_sign(libssh2_dsa_ctx *dsa, - const unsigned char *hash, - size_t hash_len, - unsigned char *sig_fixed) +int ssh2_wcng_dsa_sha1_sign(ssh2_dsa_ctx *dsa, + const unsigned char *hash, + size_t hash_len, + unsigned char *sig_fixed) { unsigned char *data, *sig; ULONG cbData, datalen, siglen; @@ -1885,7 +1859,7 @@ int _libssh2_wincng_dsa_sha1_sign(libssh2_dsa_ctx *dsa, memcpy(sig_fixed, sig, siglen); } - wincng_safe_free(sig, siglen); + wcng_safe_free(sig, siglen); } else ret = (NTSTATUS)STATUS_NO_MEMORY; @@ -1894,12 +1868,12 @@ int _libssh2_wincng_dsa_sha1_sign(libssh2_dsa_ctx *dsa, ret = (NTSTATUS)STATUS_NO_MEMORY; } - wincng_safe_free(data, datalen); + wcng_safe_free(data, datalen); return BCRYPT_SUCCESS(ret) ? 0 : -1; } -void _libssh2_wincng_dsa_free(libssh2_dsa_ctx *dsa) +void ssh2_wcng_dsa_free(ssh2_dsa_ctx *dsa) { if(!dsa) return; @@ -1907,8 +1881,8 @@ void _libssh2_wincng_dsa_free(libssh2_dsa_ctx *dsa) BCryptDestroyKey(dsa->hKey); dsa->hKey = NULL; - wincng_safe_free(dsa->pbKeyObject, dsa->cbKeyObject); - wincng_safe_free(dsa, sizeof(libssh2_dsa_ctx)); + wcng_safe_free(dsa->pbKeyObject, dsa->cbKeyObject); + wcng_safe_free(dsa, sizeof(ssh2_dsa_ctx)); } #endif @@ -1922,7 +1896,7 @@ void _libssh2_wincng_dsa_free(libssh2_dsa_ctx *dsa) /* * Decode an uncompressed point. */ -static int wincng_ecdsa_decode_uncompressed_point( +static int wcng_ecdsa_decode_uncompressed_point( IN const unsigned char *encoded_point, IN size_t encoded_point_len, OUT struct ecdsa_point *point) @@ -1938,17 +1912,17 @@ static int wincng_ecdsa_decode_uncompressed_point( return LIBSSH2_ERROR_INVAL; } - for(curve = 0; curve < ARRAY_SIZE(wincng_ecdsa_algorithms); curve++) { - if(wincng_ecdsa_algorithms[curve].point_length == + for(curve = 0; curve < SSH2_ARRAYSIZE(wcng_ecdsa_algs); curve++) { + if(wcng_ecdsa_algs[curve].point_length == (encoded_point_len - 1) / 2) { point->curve = curve; point->x = encoded_point + 1; - point->x_len = wincng_ecdsa_algorithms[curve].point_length; + point->x_len = wcng_ecdsa_algs[curve].point_length; point->y = point->x + point->x_len; - point->y_len = wincng_ecdsa_algorithms[curve].point_length; + point->y_len = wcng_ecdsa_algs[curve].point_length; return LIBSSH2_ERROR_NONE; } @@ -1963,13 +1937,13 @@ static int wincng_ecdsa_decode_uncompressed_point( * The IEEE P-1363 format is defined as r || s, * where r and s are of the same length. */ -static int wincng_p1363signature_from_point(IN const unsigned char *r, - IN size_t r_len, - IN const unsigned char *s, - IN size_t s_len, - IN libssh2_curve_type curve, - OUT PUCHAR *signature, - OUT size_t *signature_length) +static int wcng_p1363signature_from_point(IN const unsigned char *r, + IN size_t r_len, + IN const unsigned char *s, + IN size_t s_len, + IN ssh2_curve_type curve, + OUT PUCHAR *signature, + OUT size_t *signature_length) { const unsigned char *r_trimmed; const unsigned char *s_trimmed; @@ -1977,13 +1951,12 @@ static int wincng_p1363signature_from_point(IN const unsigned char *r, size_t s_trimmed_len; /* Validate parameters */ - if(curve >= ARRAY_SIZE(wincng_ecdsa_algorithms)) { + if(curve >= SSH2_ARRAYSIZE(wcng_ecdsa_algs)) { return LIBSSH2_ERROR_INVAL; } *signature = NULL; - *signature_length = (size_t) - wincng_ecdsa_algorithms[curve].point_length * 2; + *signature_length = (size_t)wcng_ecdsa_algs[curve].point_length * 2; /* Trim leading zero, if any */ r_trimmed = r; @@ -2023,9 +1996,9 @@ static int wincng_p1363signature_from_point(IN const unsigned char *r, /* * Create a CNG public key from an ECC point. */ -static int wincng_publickey_from_point(IN wincng_ecc_keytype keytype, - IN struct ecdsa_point *point, - OUT BCRYPT_KEY_HANDLE *key) +static int wcng_publickey_from_point(IN wcng_ecc_keytype keytype, + IN struct ecdsa_point *point, + OUT BCRYPT_KEY_HANDLE *key) { int result = LIBSSH2_ERROR_NONE; NTSTATUS status; @@ -2053,7 +2026,7 @@ static int wincng_publickey_from_point(IN wincng_ecc_keytype keytype, ecc_blob->cbKey = point->x_len; ecc_blob->dwMagic = - wincng_ecdsa_algorithms[point->curve].public_import_magic[keytype]; + wcng_ecdsa_algs[point->curve].public_import_magic[keytype]; /** Copy x, y */ memcpy((PUCHAR)ecc_blob + sizeof(BCRYPT_ECCKEY_BLOB), @@ -2062,9 +2035,9 @@ static int wincng_publickey_from_point(IN wincng_ecc_keytype keytype, point->y, point->y_len); status = BCryptImportKeyPair( - keytype == WINCNG_ECC_KEYTYPE_ECDSA - ? wincng_ctx.hAlgECDSA[point->curve] - : wincng_ctx.hAlgECDH[point->curve], + keytype == WCNG_ECC_KEYTYPE_ECDSA + ? ssh2_wcng.hAlgECDSA[point->curve] + : ssh2_wcng.hAlgECDH[point->curve], NULL, BCRYPT_ECCPUBLIC_BLOB, key, @@ -2086,11 +2059,11 @@ cleanup: /* * Create a CNG private key from an ECC point. */ -static int wincng_privatekey_from_point(IN wincng_ecc_keytype keytype, - IN struct ecdsa_point *q, - IN unsigned char *d, - IN size_t d_len, - OUT BCRYPT_KEY_HANDLE *key) +static int wcng_privatekey_from_point(IN wcng_ecc_keytype keytype, + IN struct ecdsa_point *q, + IN unsigned char *d, + IN size_t d_len, + OUT BCRYPT_KEY_HANDLE *key) { int result = LIBSSH2_ERROR_NONE; NTSTATUS status; @@ -2119,7 +2092,7 @@ static int wincng_privatekey_from_point(IN wincng_ecc_keytype keytype, ecc_blob->cbKey = q->x_len; ecc_blob->dwMagic = - wincng_ecdsa_algorithms[q->curve].private_import_magic[keytype]; + wcng_ecdsa_algs[q->curve].private_import_magic[keytype]; /* Copy x, y, d */ memcpy((PUCHAR)ecc_blob + sizeof(BCRYPT_ECCKEY_BLOB), @@ -2130,9 +2103,9 @@ static int wincng_privatekey_from_point(IN wincng_ecc_keytype keytype, d, d_len); status = BCryptImportKeyPair( - keytype == WINCNG_ECC_KEYTYPE_ECDSA - ? wincng_ctx.hAlgECDSA[q->curve] - : wincng_ctx.hAlgECDH[q->curve], + keytype == WCNG_ECC_KEYTYPE_ECDSA + ? ssh2_wcng.hAlgECDSA[q->curve] + : ssh2_wcng.hAlgECDH[q->curve], NULL, BCRYPT_ECCPRIVATE_BLOB, key, @@ -2154,9 +2127,9 @@ cleanup: /* * Get the uncompressed point encoding for a CNG key. */ -static int wincng_uncompressed_point_from_publickey( +static int wcng_uncompressed_point_from_publickey( IN LIBSSH2_SESSION *session, - IN libssh2_curve_type curve, + IN ssh2_curve_type curve, IN BCRYPT_KEY_HANDLE key, OUT PUCHAR *encoded_point, OUT size_t *encoded_point_len) @@ -2170,7 +2143,7 @@ static int wincng_uncompressed_point_from_publickey( PUCHAR point_y; /* Validate parameters */ - if(curve >= ARRAY_SIZE(wincng_ecdsa_algorithms)) { + if(curve >= SSH2_ARRAYSIZE(wcng_ecdsa_algs)) { return LIBSSH2_ERROR_INVAL; } @@ -2192,7 +2165,7 @@ static int wincng_uncompressed_point_from_publickey( &ecc_blob_len, 0); if(BCRYPT_SUCCESS(status) && ecc_blob_len > 0) { - ecc_blob = LIBSSH2_ALLOC(session, ecc_blob_len); + ecc_blob = SSH2_ALLOC(session, ecc_blob_len); if(!ecc_blob) { result = LIBSSH2_ERROR_ALLOC; goto cleanup; @@ -2208,8 +2181,8 @@ static int wincng_uncompressed_point_from_publickey( } if(!BCRYPT_SUCCESS(status)) { - result = _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Decoding the ECC public key failed"); + result = ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Decoding the ECC public key failed"); goto cleanup; } @@ -2227,7 +2200,7 @@ static int wincng_uncompressed_point_from_publickey( */ *encoded_point_len = (size_t)ecc_blob->cbKey * 2 + 1; - *encoded_point = LIBSSH2_ALLOC(session, *encoded_point_len); + *encoded_point = SSH2_ALLOC(session, *encoded_point_len); if(!*encoded_point) { result = LIBSSH2_ERROR_ALLOC; goto cleanup; @@ -2239,14 +2212,13 @@ static int wincng_uncompressed_point_from_publickey( cleanup: if(ecc_blob) { - LIBSSH2_FREE(session, ecc_blob); + SSH2_FREE(session, ecc_blob); } return result; } -static void wincng_reverse_bytes(IN PUCHAR buffer, - IN size_t buffer_len) +static void wcng_reverse_bytes(IN PUCHAR buffer, IN size_t buffer_len) { PUCHAR start = buffer; PUCHAR end = buffer + buffer_len - 1; @@ -2263,7 +2235,7 @@ static void wincng_reverse_bytes(IN PUCHAR buffer, /* * Windows CNG backend: ECDSA functions */ -void _libssh2_wincng_ecdsa_free(IN struct wincng_ecdsa_ctx *key) +void ssh2_wcng_ecdsa_free(IN struct wcng_ecdsa_ctx *key) { if(!key) { return; @@ -2277,11 +2249,11 @@ void _libssh2_wincng_ecdsa_free(IN struct wincng_ecdsa_ctx *key) * Creates a local private ECDH key based on input curve * and returns the public key in uncompressed point encoding. */ -int _libssh2_wincng_ecdh_create_key(IN LIBSSH2_SESSION *session, - OUT struct wincng_ecdsa_ctx **privatekey, - OUT unsigned char **encoded_publickey, - OUT size_t *encoded_publickey_len, - IN libssh2_curve_type curve) +int ssh2_wcng_ecdh_create_key(IN LIBSSH2_SESSION *session, + OUT struct wcng_ecdsa_ctx **privatekey, + OUT unsigned char **encoded_publickey, + OUT size_t *encoded_publickey_len, + IN ssh2_curve_type curve) { int result = LIBSSH2_ERROR_NONE; NTSTATUS status; @@ -2289,11 +2261,11 @@ int _libssh2_wincng_ecdh_create_key(IN LIBSSH2_SESSION *session, BCRYPT_KEY_HANDLE key_handle = NULL; /* Validate parameters */ - if(curve >= ARRAY_SIZE(wincng_ecdsa_algorithms)) { + if(curve >= SSH2_ARRAYSIZE(wcng_ecdsa_algs)) { return LIBSSH2_ERROR_INVAL; } - if(!wincng_ctx.hAlgECDH[curve]) { + if(!ssh2_wcng.hAlgECDH[curve]) { return LIBSSH2_ERROR_INVAL; } @@ -2307,35 +2279,35 @@ int _libssh2_wincng_ecdh_create_key(IN LIBSSH2_SESSION *session, /* Create an ECDH key pair using the requested curve */ status = BCryptGenerateKeyPair( - wincng_ctx.hAlgECDH[curve], + ssh2_wcng.hAlgECDH[curve], &key_handle, - wincng_ecdsa_algorithms[curve].key_length, + wcng_ecdsa_algs[curve].key_length, 0); if(!BCRYPT_SUCCESS(status)) { - result = _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Creating ECC key pair failed"); + result = ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Creating ECC key pair failed"); goto cleanup; } status = BCryptFinalizeKeyPair(key_handle, 0); if(!BCRYPT_SUCCESS(status)) { - result = _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Creating ECDH key pair failed"); + result = ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Creating ECDH key pair failed"); goto cleanup; } - result = wincng_uncompressed_point_from_publickey( + result = wcng_uncompressed_point_from_publickey( session, curve, key_handle, encoded_publickey, encoded_publickey_len); if(result != LIBSSH2_ERROR_NONE) { - result = _libssh2_error(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, - "Exporting ECDH key pair failed"); + result = ssh2_err(session, LIBSSH2_ERROR_PUBLICKEY_PROTOCOL, + "Exporting ECDH key pair failed"); } - *privatekey = malloc(sizeof(struct wincng_ecdsa_ctx)); + *privatekey = malloc(sizeof(struct wcng_ecdsa_ctx)); if(!*privatekey) { result = LIBSSH2_ERROR_ALLOC; goto cleanup; @@ -2359,11 +2331,11 @@ cleanup: /* * Creates an ECDSA public key from an uncompressed point. */ -int _libssh2_wincng_ecdsa_curve_name_with_octal_new( - OUT struct wincng_ecdsa_ctx **key, +int ssh2_wcng_ecdsa_curve_name_with_octal_new( + OUT struct wcng_ecdsa_ctx **key, IN const unsigned char *publickey_encoded, IN size_t publickey_encoded_len, - IN libssh2_curve_type curve) + IN ssh2_curve_type curve) { int result = LIBSSH2_ERROR_NONE; @@ -2371,7 +2343,7 @@ int _libssh2_wincng_ecdsa_curve_name_with_octal_new( struct ecdsa_point publickey; /* Validate parameters */ - if(curve >= ARRAY_SIZE(wincng_ecdsa_algorithms)) { + if(curve >= SSH2_ARRAYSIZE(wcng_ecdsa_algs)) { return LIBSSH2_ERROR_INVAL; } @@ -2381,7 +2353,7 @@ int _libssh2_wincng_ecdsa_curve_name_with_octal_new( *key = NULL; - result = wincng_ecdsa_decode_uncompressed_point( + result = wcng_ecdsa_decode_uncompressed_point( publickey_encoded, publickey_encoded_len, &publickey); @@ -2389,15 +2361,15 @@ int _libssh2_wincng_ecdsa_curve_name_with_octal_new( goto cleanup; } - result = wincng_publickey_from_point( - WINCNG_ECC_KEYTYPE_ECDSA, + result = wcng_publickey_from_point( + WCNG_ECC_KEYTYPE_ECDSA, &publickey, &publickey_handle); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } - *key = malloc(sizeof(struct wincng_ecdsa_ctx)); + *key = malloc(sizeof(struct wcng_ecdsa_ctx)); if(!*key) { result = LIBSSH2_ERROR_ALLOC; goto cleanup; @@ -2415,11 +2387,10 @@ cleanup: * Computes the shared secret K given a local private key, * remote public key and length */ -int _libssh2_wincng_ecdh_gen_k( - OUT libssh2_bn **secret, - IN struct wincng_ecdsa_ctx *privatekey, - IN const unsigned char *server_publickey_encoded, - IN size_t server_publickey_encoded_len) +int ssh2_wcng_ecdh_gen_k(OUT ssh2_bn **secret, + IN struct wcng_ecdsa_ctx *privatekey, + IN const unsigned char *server_publickey_encoded, + IN size_t server_publickey_encoded_len) { int result = LIBSSH2_ERROR_NONE; NTSTATUS status; @@ -2437,7 +2408,7 @@ int _libssh2_wincng_ecdh_gen_k( *secret = NULL; /* Decode the public key */ - result = wincng_ecdsa_decode_uncompressed_point( + result = wcng_ecdsa_decode_uncompressed_point( server_publickey_encoded, server_publickey_encoded_len, &server_publickey); @@ -2445,8 +2416,8 @@ int _libssh2_wincng_ecdh_gen_k( return result; } - result = wincng_publickey_from_point( - WINCNG_ECC_KEYTYPE_ECDH, + result = wcng_publickey_from_point( + WCNG_ECC_KEYTYPE_ECDH, &server_publickey, &publickey_handle); if(result != LIBSSH2_ERROR_NONE) { @@ -2484,13 +2455,13 @@ int _libssh2_wincng_ecdh_gen_k( } /* Allocate a secret bignum to be ready to receive the derived secret */ - *secret = _libssh2_wincng_bignum_init(); + *secret = ssh2_wcng_bn_init(); if(!*secret) { result = LIBSSH2_ERROR_ALLOC; goto cleanup; } - if(wincng_bignum_resize(*secret, secret_len)) { + if(wcng_bn_resize(*secret, secret_len)) { result = LIBSSH2_ERROR_ALLOC; goto cleanup; } @@ -2513,13 +2484,13 @@ int _libssh2_wincng_ecdh_gen_k( * raw secret, so we need to swap it to big endian order. */ - wincng_reverse_bytes((*secret)->bignum, secret_len); + wcng_reverse_bytes((*secret)->bignum, secret_len); result = LIBSSH2_ERROR_NONE; cleanup: if(result != LIBSSH2_ERROR_NONE && agreed_secret_handle) { - _libssh2_wincng_bignum_free(*secret); + ssh2_wcng_bn_free(*secret); } if(result != LIBSSH2_ERROR_NONE && agreed_secret_handle) { @@ -2529,8 +2500,8 @@ cleanup: return result; } -static int wincng_ecdsa_curve_type_from_name(IN const char *name, - OUT libssh2_curve_type *out_curve) +static int wcng_ecdsa_curve_type_from_name(IN const char *name, + OUT ssh2_curve_type *out_curve) { unsigned int curve; @@ -2539,8 +2510,8 @@ static int wincng_ecdsa_curve_type_from_name(IN const char *name, return LIBSSH2_ERROR_INVAL; } - for(curve = 0; curve < ARRAY_SIZE(wincng_ecdsa_algorithms); curve++) { - if(strcmp(name, wincng_ecdsa_algorithms[curve].name) == 0) { + for(curve = 0; curve < SSH2_ARRAYSIZE(wcng_ecdsa_algs); curve++) { + if(strcmp(name, wcng_ecdsa_algs[curve].name) == 0) { *out_curve = curve; return LIBSSH2_ERROR_NONE; } @@ -2552,13 +2523,13 @@ static int wincng_ecdsa_curve_type_from_name(IN const char *name, /* * Verifies the ECDSA signature of a hashed message */ -int _libssh2_wincng_ecdsa_verify(IN struct wincng_ecdsa_ctx *key, - IN const unsigned char *r, - IN size_t r_len, - IN const unsigned char *s, - IN size_t s_len, - IN const unsigned char *m, - IN size_t m_len) +int ssh2_wcng_ecdsa_verify(IN struct wcng_ecdsa_ctx *key, + IN const unsigned char *r, + IN size_t r_len, + IN const unsigned char *s, + IN size_t s_len, + IN const unsigned char *m, + IN size_t m_len) { int result = LIBSSH2_ERROR_NONE; NTSTATUS status; @@ -2570,12 +2541,12 @@ int _libssh2_wincng_ecdsa_verify(IN struct wincng_ecdsa_ctx *key, BCRYPT_ALG_HANDLE hash_alg; /* CNG expects signatures in IEEE P-1363 format. */ - result = wincng_p1363signature_from_point( + result = wcng_p1363signature_from_point( r, r_len, s, s_len, - _libssh2_wincng_ecdsa_get_curve_type(key), + ssh2_wcng_ecdsa_get_curve_type(key), &signature_p1363, &signature_p1363_len); if(result != LIBSSH2_ERROR_NONE) { @@ -2583,20 +2554,20 @@ int _libssh2_wincng_ecdsa_verify(IN struct wincng_ecdsa_ctx *key, } /* Create hash over m */ - switch(_libssh2_wincng_ecdsa_get_curve_type(key)) { - case LIBSSH2_EC_CURVE_NISTP256: + switch(ssh2_wcng_ecdsa_get_curve_type(key)) { + case SSH2_EC_CURVE_NISTP256: hash_len = 256 / 8; - hash_alg = wincng_ctx.hAlgHashSHA256; + hash_alg = ssh2_wcng.hAlgHashSHA256; break; - case LIBSSH2_EC_CURVE_NISTP384: + case SSH2_EC_CURVE_NISTP384: hash_len = 384 / 8; - hash_alg = wincng_ctx.hAlgHashSHA384; + hash_alg = ssh2_wcng.hAlgHashSHA384; break; - case LIBSSH2_EC_CURVE_NISTP521: + case SSH2_EC_CURVE_NISTP521: hash_len = 512 / 8; - hash_alg = wincng_ctx.hAlgHashSHA512; + hash_alg = ssh2_wcng.hAlgHashSHA512; break; default: @@ -2604,12 +2575,7 @@ int _libssh2_wincng_ecdsa_verify(IN struct wincng_ecdsa_ctx *key, } hash = malloc(hash_len); - result = _libssh2_wincng_hash( - m, - (ULONG)m_len, - hash_alg, - hash, - hash_len); + result = ssh2_wcng_hash(m, (ULONG)m_len, hash_alg, hash, hash_len); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } @@ -2650,10 +2616,10 @@ cleanup: /* * Creates a new private key given a file path and password */ -int _libssh2_wincng_ecdsa_new_private(OUT struct wincng_ecdsa_ctx **key, - IN LIBSSH2_SESSION *session, - IN const char *filename, - IN const unsigned char *passphrase) +int ssh2_wcng_ecdsa_new_private(OUT struct wcng_ecdsa_ctx **key, + IN LIBSSH2_SESSION *session, + IN const char *filename, + IN const unsigned char *passphrase) { int result; @@ -2669,19 +2635,19 @@ int _libssh2_wincng_ecdsa_new_private(OUT struct wincng_ecdsa_ctx **key, *key = NULL; if(passphrase && strlen((const char *)passphrase) > 0) { - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Passphrase-protected ECDSA private key " - "files are unsupported"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Passphrase-protected ECDSA private key " + "files are unsupported"); } file_handle = fopen(filename, "rb"); if(!file_handle) { - result = _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Opening the private key file failed"); + result = ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Opening the private key file failed"); goto cleanup; } - result = _libssh2_pem_parse(session, + result = ssh2_pem_parse(session, PEM_ECDSA_HEADER, PEM_ECDSA_FOOTER, passphrase, @@ -2692,7 +2658,7 @@ int _libssh2_wincng_ecdsa_new_private(OUT struct wincng_ecdsa_ctx **key, goto cleanup; } - result = _libssh2_wincng_ecdsa_new_private_frommemory( + result = ssh2_wcng_ecdsa_new_private_frommemory( key, session, (const char *)data, @@ -2708,15 +2674,15 @@ cleanup: } if(data) { - LIBSSH2_FREE(session, data); + SSH2_FREE(session, data); } return result; } -static int wincng_parse_ecdsa_privatekey(OUT struct wincng_ecdsa_ctx **key, - IN unsigned char *privatekey, - IN size_t privatekey_len) +static int wcng_parse_ecdsa_privatekey(OUT struct wcng_ecdsa_ctx **key, + IN unsigned char *privatekey, + IN size_t privatekey_len) { char *keytype = NULL; size_t keytype_len; @@ -2727,7 +2693,7 @@ static int wincng_parse_ecdsa_privatekey(OUT struct wincng_ecdsa_ctx **key, unsigned char *publickey; size_t publickey_len; - libssh2_curve_type curve_type; + ssh2_curve_type curve_type; int result; uint32_t check1, check2; struct string_buf data_buffer; @@ -2745,12 +2711,12 @@ static int wincng_parse_ecdsa_privatekey(OUT struct wincng_ecdsa_ctx **key, data_buffer.len = privatekey_len; /* Read the 2 checkints and check that they match */ - result = _libssh2_get_u32(&data_buffer, &check1); + result = ssh2_get_u32(&data_buffer, &check1); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } - result = _libssh2_get_u32(&data_buffer, &check2); + result = ssh2_get_u32(&data_buffer, &check2); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } @@ -2764,31 +2730,30 @@ static int wincng_parse_ecdsa_privatekey(OUT struct wincng_ecdsa_ctx **key, /* draft-miller-ssh-agent, section-3.2.2 */ /* Read the key type */ - result = _libssh2_get_string(&data_buffer, - (unsigned char **)&keytype, - &keytype_len); + result = ssh2_get_string(&data_buffer, + (unsigned char **)&keytype, &keytype_len); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } - result = wincng_ecdsa_curve_type_from_name(keytype, &curve_type); + result = wcng_ecdsa_curve_type_from_name(keytype, &curve_type); if(result < 0) { goto cleanup; } /* Read the curve */ - result = _libssh2_get_string(&data_buffer, &ignore, &ignore_len); + result = ssh2_get_string(&data_buffer, &ignore, &ignore_len); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } /* Read Q */ - result = _libssh2_get_string(&data_buffer, &publickey, &publickey_len); + result = ssh2_get_string(&data_buffer, &publickey, &publickey_len); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } - result = wincng_ecdsa_decode_uncompressed_point( + result = wcng_ecdsa_decode_uncompressed_point( publickey, publickey_len, &q); @@ -2797,7 +2762,7 @@ static int wincng_parse_ecdsa_privatekey(OUT struct wincng_ecdsa_ctx **key, } /* Read d */ - result = _libssh2_get_bignum_bytes(&data_buffer, &d, &d_len); + result = ssh2_get_bignum_bytes(&data_buffer, &d, &d_len); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } @@ -2805,8 +2770,8 @@ static int wincng_parse_ecdsa_privatekey(OUT struct wincng_ecdsa_ctx **key, /* Ignore the rest (comment, etc) */ /* Use Q and d to create a key handle */ - result = wincng_privatekey_from_point( - WINCNG_ECC_KEYTYPE_ECDSA, + result = wcng_privatekey_from_point( + WCNG_ECC_KEYTYPE_ECDSA, &q, d, d_len, @@ -2815,7 +2780,7 @@ static int wincng_parse_ecdsa_privatekey(OUT struct wincng_ecdsa_ctx **key, goto cleanup; } - *key = malloc(sizeof(struct wincng_ecdsa_ctx)); + *key = malloc(sizeof(struct wcng_ecdsa_ctx)); if(!*key) { result = LIBSSH2_ERROR_ALLOC; goto cleanup; @@ -2839,12 +2804,11 @@ cleanup: * ECDSA private key files use the decoding defined in PROTOCOL.key * in the OpenSSL source tree. */ -int _libssh2_wincng_ecdsa_new_private_frommemory( - OUT struct wincng_ecdsa_ctx **key, - IN LIBSSH2_SESSION *session, - IN const char *data, - IN size_t data_len, - IN const unsigned char *passphrase) +int ssh2_wcng_ecdsa_new_private_frommemory(OUT struct wcng_ecdsa_ctx **key, + IN LIBSSH2_SESSION *session, + IN const char *data, + IN size_t data_len, + IN const unsigned char *passphrase) { int result; @@ -2862,9 +2826,9 @@ int _libssh2_wincng_ecdsa_new_private_frommemory( *key = NULL; if(passphrase && strlen((const char *)passphrase) > 0) { - return _libssh2_error(session, LIBSSH2_ERROR_INVAL, - "Passphrase-protected ECDSA private key " - "files are unsupported"); + return ssh2_err(session, LIBSSH2_ERROR_INVAL, + "Passphrase-protected ECDSA private key " + "files are unsupported"); } /* Read OPENSSL_PRIVATEKEY_AUTH_MAGIC */ @@ -2874,30 +2838,30 @@ int _libssh2_wincng_ecdsa_new_private_frommemory( } data_buffer.len = data_len; - data_buffer.data = (unsigned char *)LIBSSH2_UNCONST(data); + data_buffer.data = (unsigned char *)SSH2_UNCONST(data); data_buffer.dataptr = data_buffer.data + strlen(OPENSSL_PRIVATEKEY_AUTH_MAGIC) + 1; /* Read ciphername, should be 'none' as we do not support passphrases */ - result = _libssh2_match_string(&data_buffer, "none"); + result = ssh2_match_string(&data_buffer, "none"); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } /* Read kdfname, should be 'none' as we do not support passphrases */ - result = _libssh2_match_string(&data_buffer, "none"); + result = ssh2_match_string(&data_buffer, "none"); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } /* Read kdfoptions, should be empty */ - result = _libssh2_match_string(&data_buffer, ""); + result = ssh2_match_string(&data_buffer, ""); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } /* Read number of keys N */ - result = _libssh2_get_u32(&data_buffer, &key_count); + result = ssh2_get_u32(&data_buffer, &key_count); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } @@ -2912,23 +2876,23 @@ int _libssh2_wincng_ecdsa_new_private_frommemory( unsigned char *publickey; size_t publickey_len; - result = _libssh2_get_string(&data_buffer, &publickey, &publickey_len); + result = ssh2_get_string(&data_buffer, &publickey, &publickey_len); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } } /* Read first private key */ - result = _libssh2_get_string(&data_buffer, &privatekey, &privatekey_len); + result = ssh2_get_string(&data_buffer, &privatekey, &privatekey_len); if(result != LIBSSH2_ERROR_NONE) { goto cleanup; } - result = wincng_parse_ecdsa_privatekey(key, privatekey, privatekey_len); + result = wcng_parse_ecdsa_privatekey(key, privatekey, privatekey_len); cleanup: if(result != LIBSSH2_ERROR_NONE) { - return _libssh2_error(session, result, "The key is malformed"); + return ssh2_err(session, result, "The key is malformed"); } return result; @@ -2937,12 +2901,12 @@ cleanup: /* * Computes the ECDSA signature of a previously-hashed message */ -int _libssh2_wincng_ecdsa_sign(IN LIBSSH2_SESSION *session, - IN struct wincng_ecdsa_ctx *key, - IN const unsigned char *hash, - IN size_t hash_len, - OUT unsigned char **signature, - OUT size_t *signature_len) +int ssh2_wcng_ecdsa_sign(IN LIBSSH2_SESSION *session, + IN struct wcng_ecdsa_ctx *key, + IN const unsigned char *hash, + IN size_t hash_len, + OUT unsigned char **signature, + OUT size_t *signature_len) { NTSTATUS status; int result = LIBSSH2_ERROR_NONE; @@ -3010,27 +2974,26 @@ int _libssh2_wincng_ecdsa_sign(IN LIBSSH2_SESSION *session, cng_signature_len / 2 + 5 + /* mpint(r) */ cng_signature_len / 2 + 5; /* mpint(s) */ - *signature = LIBSSH2_ALLOC(session, signature_maxlen); + *signature = SSH2_ALLOC(session, signature_maxlen); signature_ptr = *signature; - if(_libssh2_store_bignum2_bytes(&signature_ptr, - cng_signature, - cng_signature_len / 2) && - _libssh2_store_bignum2_bytes(&signature_ptr, - cng_signature + (cng_signature_len / 2), - cng_signature_len / 2)) { + if(ssh2_store_bignum_bytes(&signature_ptr, + cng_signature, + cng_signature_len / 2) && + ssh2_store_bignum_bytes(&signature_ptr, + cng_signature + (cng_signature_len / 2), + cng_signature_len / 2)) { *signature_len = signature_ptr - *signature; } else { - _libssh2_debug((session, LIBSSH2_ERROR_STORE_OVERFLOW, - "Too large write.")); + ssh2_deb((session, LIBSSH2_ERROR_STORE_OVERFLOW, "Too large write.")); result = LIBSSH2_ERROR_STORE_OVERFLOW; goto cleanup; } cleanup: if(result != LIBSSH2_ERROR_NONE && *signature) { - LIBSSH2_FREE(session, *signature); + SSH2_FREE(session, *signature); *signature = NULL; *signature_len = 0; } @@ -3047,10 +3010,9 @@ cleanup: } /* - * returns key curve type that maps to libssh2_curve_type + * returns key curve type that maps to ssh2_curve_type */ -libssh2_curve_type _libssh2_wincng_ecdsa_get_curve_type( - IN struct wincng_ecdsa_ctx *key) +ssh2_curve_type ssh2_wcng_ecdsa_get_curve_type(IN struct wcng_ecdsa_ctx *key) { return key->curve; } @@ -3063,12 +3025,12 @@ libssh2_curve_type _libssh2_wincng_ecdsa_get_curve_type( */ #ifdef HAVE_LIBCRYPT32 -static DWORD wincng_pub_priv_write(unsigned char *key, - DWORD offset, - const unsigned char *bignum, - const DWORD length) +static DWORD wcng_pub_priv_write(unsigned char *key, + DWORD offset, + const unsigned char *bignum, + const DWORD length) { - _libssh2_htonu32(key + offset, length); + ssh2_htonu32(key + offset, length); offset += 4; memcpy(key + offset, bignum, length); @@ -3077,13 +3039,13 @@ static DWORD wincng_pub_priv_write(unsigned char *key, return offset; } -static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - unsigned char *pbEncoded, - size_t cbEncoded) +static int wcng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + unsigned char *pbEncoded, + size_t cbEncoded) { unsigned char **rpbDecoded = NULL; DWORD *rcbDecoded = NULL; @@ -3092,10 +3054,10 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, DWORD index, offset, length = 0; int ret; - ret = wincng_asn_decode_bns(pbEncoded, (DWORD)cbEncoded, - &rpbDecoded, &rcbDecoded, &length); + ret = wcng_asn_decode_bns(pbEncoded, (DWORD)cbEncoded, + &rpbDecoded, &rcbDecoded, &length); - wincng_safe_free(pbEncoded, cbEncoded); + wcng_safe_free(pbEncoded, cbEncoded); if(ret) { return -1; @@ -3103,7 +3065,7 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, if(length == 9) { /* private RSA key */ mthlen = 7; - mth = LIBSSH2_ALLOC(session, mthlen); + mth = SSH2_ALLOC(session, mthlen); if(mth) { memcpy(mth, "ssh-rsa", mthlen); } @@ -3112,17 +3074,17 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, } keylen = 4 + mthlen + 4 + rcbDecoded[2] + 4 + rcbDecoded[1]; - key = LIBSSH2_ALLOC(session, keylen); + key = SSH2_ALLOC(session, keylen); if(key) { - offset = wincng_pub_priv_write(key, 0, mth, mthlen); + offset = wcng_pub_priv_write(key, 0, mth, mthlen); - offset = wincng_pub_priv_write(key, offset, - rpbDecoded[2], - rcbDecoded[2]); + offset = wcng_pub_priv_write(key, offset, + rpbDecoded[2], + rcbDecoded[2]); - wincng_pub_priv_write(key, offset, - rpbDecoded[1], - rcbDecoded[1]); + wcng_pub_priv_write(key, offset, + rpbDecoded[1], + rcbDecoded[1]); } else { ret = -1; @@ -3130,7 +3092,7 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, } else if(length == 6) { /* private DSA key */ mthlen = 7; - mth = LIBSSH2_ALLOC(session, mthlen); + mth = SSH2_ALLOC(session, mthlen); if(mth) { memcpy(mth, "ssh-dss", mthlen); } @@ -3140,25 +3102,25 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, keylen = 4 + mthlen + 4 + rcbDecoded[1] + 4 + rcbDecoded[2] + 4 + rcbDecoded[3] + 4 + rcbDecoded[4]; - key = LIBSSH2_ALLOC(session, keylen); + key = SSH2_ALLOC(session, keylen); if(key) { - offset = wincng_pub_priv_write(key, 0, mth, mthlen); + offset = wcng_pub_priv_write(key, 0, mth, mthlen); - offset = wincng_pub_priv_write(key, offset, - rpbDecoded[1], - rcbDecoded[1]); + offset = wcng_pub_priv_write(key, offset, + rpbDecoded[1], + rcbDecoded[1]); - offset = wincng_pub_priv_write(key, offset, - rpbDecoded[2], - rcbDecoded[2]); + offset = wcng_pub_priv_write(key, offset, + rpbDecoded[2], + rcbDecoded[2]); - offset = wincng_pub_priv_write(key, offset, - rpbDecoded[3], - rcbDecoded[3]); + offset = wcng_pub_priv_write(key, offset, + rpbDecoded[3], + rcbDecoded[3]); - wincng_pub_priv_write(key, offset, - rpbDecoded[4], - rcbDecoded[4]); + wcng_pub_priv_write(key, offset, + rpbDecoded[4], + rcbDecoded[4]); } else { ret = -1; @@ -3169,7 +3131,7 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, } for(index = 0; index < length; index++) { - wincng_safe_free(rpbDecoded[index], rcbDecoded[index]); + wcng_safe_free(rpbDecoded[index], rcbDecoded[index]); rpbDecoded[index] = NULL; rcbDecoded[index] = 0; } @@ -3179,9 +3141,9 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, if(ret) { if(mth) - LIBSSH2_FREE(session, mth); + SSH2_FREE(session, mth); if(key) - LIBSSH2_FREE(session, key); + SSH2_FREE(session, key); } else { *method = mth; @@ -3194,29 +3156,29 @@ static int wincng_pub_priv_keyfile_parse(LIBSSH2_SESSION *session, } #endif /* HAVE_LIBCRYPT32 */ -int _libssh2_wincng_pub_priv_keyfile(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekey, - const char *passphrase) +int ssh2_wcng_pub_priv_keyfile(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekey, + const char *passphrase) { #ifdef HAVE_LIBCRYPT32 unsigned char *pbEncoded; size_t cbEncoded; int ret; - ret = wincng_load_private(session, privatekey, - (const unsigned char *)passphrase, - &pbEncoded, &cbEncoded, 1, 1); + ret = wcng_load_private(session, privatekey, + (const unsigned char *)passphrase, + &pbEncoded, &cbEncoded, 1, 1); if(ret) { return -1; } - return wincng_pub_priv_keyfile_parse(session, method, method_len, - pubkeydata, pubkeydata_len, - pbEncoded, cbEncoded); + return wcng_pub_priv_keyfile_parse(session, method, method_len, + pubkeydata, pubkeydata_len, + pbEncoded, cbEncoded); #else (void)method; (void)method_len; @@ -3225,37 +3187,37 @@ int _libssh2_wincng_pub_priv_keyfile(LIBSSH2_SESSION *session, (void)privatekey; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to load public key from private key file: " - "Method unsupported in Windows CNG backend"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to load public key from private key file: " + "Method unsupported in Windows CNG backend"); #endif /* HAVE_LIBCRYPT32 */ } -int _libssh2_wincng_pub_priv_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_wcng_pub_priv_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { #ifdef HAVE_LIBCRYPT32 unsigned char *pbEncoded; size_t cbEncoded; int ret; - ret = wincng_load_private_memory(session, privatekeydata, - privatekeydata_len, - (const unsigned char *)passphrase, - &pbEncoded, &cbEncoded, 1, 1); + ret = wcng_load_private_memory(session, privatekeydata, + privatekeydata_len, + (const unsigned char *)passphrase, + &pbEncoded, &cbEncoded, 1, 1); if(ret) { return -1; } - return wincng_pub_priv_keyfile_parse(session, method, method_len, - pubkeydata, pubkeydata_len, - pbEncoded, cbEncoded); + return wcng_pub_priv_keyfile_parse(session, method, method_len, + pubkeydata, pubkeydata_len, + pbEncoded, cbEncoded); #else (void)method; (void)method_len; @@ -3265,25 +3227,25 @@ int _libssh2_wincng_pub_priv_keyfilememory(LIBSSH2_SESSION *session, (void)privatekeydata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, - "Unable to extract public key from private key in " - "memory: Method unsupported in Windows CNG backend"); + return ssh2_err(session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, + "Unable to extract public key from private key in " + "memory: Method unsupported in Windows CNG backend"); #endif /* HAVE_LIBCRYPT32 */ } -int _libssh2_wincng_sk_pub_keyfilememory(LIBSSH2_SESSION *session, - unsigned char **method, - size_t *method_len, - unsigned char **pubkeydata, - size_t *pubkeydata_len, - int *algorithm, - unsigned char *flags, - const char **application, - const unsigned char **key_handle, - size_t *handle_len, - const char *privatekeydata, - size_t privatekeydata_len, - const char *passphrase) +int ssh2_wcng_sk_pub_keyfilememory(LIBSSH2_SESSION *session, + unsigned char **method, + size_t *method_len, + unsigned char **pubkeydata, + size_t *pubkeydata_len, + int *algorithm, + unsigned char *flags, + const char **application, + const unsigned char **key_handle, + size_t *handle_len, + const char *privatekeydata, + size_t privatekeydata_len, + const char *passphrase) { (void)method; (void)method_len; @@ -3298,20 +3260,18 @@ int _libssh2_wincng_sk_pub_keyfilememory(LIBSSH2_SESSION *session, (void)privatekeydata_len; (void)passphrase; - return _libssh2_error(session, LIBSSH2_ERROR_FILE, - "Unable to extract public SK key from private key " - "file: Method unimplemented in Windows CNG backend"); + return ssh2_err(session, LIBSSH2_ERROR_FILE, + "Unable to extract public SK key from private key " + "file: Method unimplemented in Windows CNG backend"); } /*******************************************************************/ /* * Windows CNG backend: Cipher functions */ -int _libssh2_wincng_cipher_init(libssh2_cipher_ctx *h, - LIBSSH2_CIPHER_T(algo), - unsigned char *iv, - unsigned char *secret, - int encrypt) +int ssh2_wcng_cipher_init(ssh2_cipher_ctx *h, SSH2_CIPHER_T(algo), + unsigned char *iv, unsigned char *secret, + int encrypt) { BCRYPT_KEY_HANDLE hKey; BCRYPT_KEY_DATA_BLOB_HEADER *header; @@ -3342,8 +3302,7 @@ int _libssh2_wincng_cipher_init(libssh2_cipher_ctx *h, return -1; } - keylen = (ULONG)sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + - algo.dwKeyLength; + keylen = (ULONG)sizeof(BCRYPT_KEY_DATA_BLOB_HEADER) + algo.dwKeyLength; header = (BCRYPT_KEY_DATA_BLOB_HEADER *)malloc(keylen); if(!header) { free(pbKeyObject); @@ -3361,10 +3320,10 @@ int _libssh2_wincng_cipher_init(libssh2_cipher_ctx *h, pbKeyObject, dwKeyObject, (PUCHAR)header, keylen, 0); - wincng_safe_free(header, keylen); + wcng_safe_free(header, keylen); if(!BCRYPT_SUCCESS(ret)) { - wincng_safe_free(pbKeyObject, dwKeyObject); + wcng_safe_free(pbKeyObject, dwKeyObject); return -1; } @@ -3377,7 +3336,7 @@ int _libssh2_wincng_cipher_init(libssh2_cipher_ctx *h, pbIVCopy = malloc(dwBlockLength); if(!pbIVCopy) { BCryptDestroyKey(hKey); - wincng_safe_free(pbKeyObject, dwKeyObject); + wcng_safe_free(pbKeyObject, dwKeyObject); return -1; } memcpy(pbIVCopy, iv, dwBlockLength); @@ -3406,7 +3365,7 @@ int _libssh2_wincng_cipher_init(libssh2_cipher_ctx *h, /* Increments an AES CTR buffer to prepare it for use with the next AES block. */ -static void aes_ctr_increment(unsigned char *ctr, size_t length) +static void wcng_aes_ctr_increment(unsigned char *ctr, size_t length) { unsigned char *pc; unsigned int val, carry; @@ -3421,11 +3380,9 @@ static void aes_ctr_increment(unsigned char *ctr, size_t length) } } -int _libssh2_wincng_cipher_crypt(libssh2_cipher_ctx *ctx, - LIBSSH2_CIPHER_T(algo), - int encrypt, - unsigned char *block, - size_t blocksize, int firstlast) +int ssh2_wcng_cipher_crypt(ssh2_cipher_ctx *ctx, SSH2_CIPHER_T(algo), + int encrypt, unsigned char *block, size_t blocksize, + int firstlast) { unsigned char *pbOutput, *pbInput; ULONG cbOutput, cbInput; @@ -3467,15 +3424,15 @@ int _libssh2_wincng_cipher_crypt(libssh2_cipher_ctx *ctx, if(BCRYPT_SUCCESS(ret)) { if(algo.ctrMode) { /* NOLINTNEXTLINE(readability-suspicious-call-argument) */ - _libssh2_xor_data(block, block, pbOutput, blocksize); - aes_ctr_increment(ctx->pbCtr, ctx->dwCtrLength); + ssh2_xor_data(block, block, pbOutput, blocksize); + wcng_aes_ctr_increment(ctx->pbCtr, ctx->dwCtrLength); } else { memcpy(block, pbOutput, cbOutput); } } - wincng_safe_free(pbOutput, cbOutput); + wcng_safe_free(pbOutput, cbOutput); } else ret = (NTSTATUS)STATUS_NO_MEMORY; @@ -3484,20 +3441,20 @@ int _libssh2_wincng_cipher_crypt(libssh2_cipher_ctx *ctx, return BCRYPT_SUCCESS(ret) ? 0 : -1; } -void _libssh2_wincng_cipher_dtor(libssh2_cipher_ctx *ctx) +void ssh2_wcng_cipher_dtor(ssh2_cipher_ctx *ctx) { BCryptDestroyKey(ctx->hKey); ctx->hKey = NULL; - wincng_safe_free(ctx->pbKeyObject, ctx->dwKeyObject); + wcng_safe_free(ctx->pbKeyObject, ctx->dwKeyObject); ctx->pbKeyObject = NULL; ctx->dwKeyObject = 0; - wincng_safe_free(ctx->pbIV, ctx->dwBlockLength); + wcng_safe_free(ctx->pbIV, ctx->dwBlockLength); ctx->pbIV = NULL; ctx->dwBlockLength = 0; - wincng_safe_free(ctx->pbCtr, ctx->dwCtrLength); + wcng_safe_free(ctx->pbCtr, ctx->dwCtrLength); ctx->pbCtr = NULL; ctx->dwCtrLength = 0; } @@ -3507,7 +3464,7 @@ void _libssh2_wincng_cipher_dtor(libssh2_cipher_ctx *ctx) * Windows CNG backend: Diffie-Hellman support. */ -void _libssh2_dh_init(struct wincng_dh_ctx *dhctx) +void ssh2_wcng_dh_init(ssh2_dh_ctx *dhctx) { /* Random from client */ dhctx->dh_handle = NULL; @@ -3515,7 +3472,7 @@ void _libssh2_dh_init(struct wincng_dh_ctx *dhctx) dhctx->dh_privbn = NULL; } -void _libssh2_dh_dtor(struct wincng_dh_ctx *dhctx) +void ssh2_wcng_dh_dtor(ssh2_dh_ctx *dhctx) { if(dhctx->dh_handle) { BCryptDestroyKey(dhctx->dh_handle); @@ -3528,12 +3485,12 @@ void _libssh2_dh_dtor(struct wincng_dh_ctx *dhctx) dhctx->dh_params = NULL; } if(dhctx->dh_privbn) { - _libssh2_wincng_bignum_free(dhctx->dh_privbn); + ssh2_wcng_bn_free(dhctx->dh_privbn); dhctx->dh_privbn = NULL; } } -static int round_down(int number, int multiple) +static int wcng_round_down(int number, int multiple) { return (number / multiple) * multiple; } @@ -3543,15 +3500,15 @@ static int round_down(int number, int multiple) * private key is stored as opaque in the Diffie-Hellman context `*dhctx' and * the public key is returned in `public'. 0 is returned upon success, else * -1. */ -int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, int group_order) +int ssh2_wcng_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, int group_order) { - const int hasAlgDHwithKDF = wincng_ctx.hasAlgDHwithKDF; + const int hasAlgDHwithKDF = ssh2_wcng.hasAlgDHwithKDF; if(group_order < 0) return -1; - while(wincng_ctx.hAlgDH && hasAlgDHwithKDF != -1) { + while(ssh2_wcng.hAlgDH && hasAlgDHwithKDF != -1) { BCRYPT_DH_PARAMETER_HEADER *dh_params; ULONG dh_params_len; int status; @@ -3559,14 +3516,14 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, * in length. At the time of writing a practical observed group_order * value is 257, so we need to round down to 8 bytes of length (64/8) * in order for kex to succeed */ - ULONG key_length_bytes = max((ULONG)round_down(group_order, 8), + ULONG key_length_bytes = max((ULONG)wcng_round_down(group_order, 8), max(g->length, p->length)); BCRYPT_DH_KEY_BLOB *dh_key_blob; LPCWSTR key_type; /* Prepare a key pair; pass the in the bit length of the key, * but the key is not ready for consumption until it is finalized. */ - status = BCryptGenerateKeyPair(wincng_ctx.hAlgDH, + status = BCryptGenerateKeyPair(ssh2_wcng.hAlgDH, &dhctx->dh_handle, key_length_bytes * 8, 0); if(!BCRYPT_SUCCESS(status)) { @@ -3584,12 +3541,12 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, dh_params->cbLength = dh_params_len; dh_params->dwMagic = BCRYPT_DH_PARAMETERS_MAGIC; dh_params->cbKeyLength = key_length_bytes; - memcpy_with_be_padding((unsigned char *)dh_params + - sizeof(*dh_params), - key_length_bytes, p->bignum, p->length); - memcpy_with_be_padding((unsigned char *)dh_params + - sizeof(*dh_params) + key_length_bytes, - key_length_bytes, g->bignum, g->length); + wcng_memcpy_with_be_padding((unsigned char *)dh_params + + sizeof(*dh_params), + key_length_bytes, p->bignum, p->length); + wcng_memcpy_with_be_padding((unsigned char *)dh_params + + sizeof(*dh_params) + key_length_bytes, + key_length_bytes, g->bignum, g->length); status = BCryptSetProperty(dhctx->dh_handle, BCRYPT_DH_PARAMETERS, (PUCHAR)dh_params, dh_params_len, 0); @@ -3601,7 +3558,7 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, /* Pass ownership to dhctx; these parameters are freed when * the context is destroyed. We need to keep the parameters more * easily available so that we have access to the `g` value when - * _libssh2_dh_secret() is called later. */ + * ssh2_wcng_dh_secret() is called later. */ dhctx->dh_params = dh_params; } dh_params = NULL; @@ -3648,7 +3605,7 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, free(dh_key_blob); } else { /* we may have potentially private data, use secure free */ - wincng_safe_free(dh_key_blob, key_length_bytes); + wcng_safe_free(dh_key_blob, key_length_bytes); } return -1; } @@ -3662,13 +3619,13 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, /* BCRYPT_DH_PUBLIC_BLOB corresponds to a BCRYPT_DH_KEY_BLOB header * followed by the Modulus, Generator and Public data. Those components * each have equal size, specified by dh_key_blob->cbKey. */ - if(wincng_bignum_resize(public, dh_key_blob->cbKey)) { + if(wcng_bn_resize(public, dh_key_blob->cbKey)) { if(hasAlgDHwithKDF == 1) { /* We have no private data, because raw KDF is supported */ free(dh_key_blob); } else { /* we may have potentially private data, use secure free */ - wincng_safe_free(dh_key_blob, key_length_bytes); + wcng_safe_free(dh_key_blob, key_length_bytes); } return -1; } @@ -3681,13 +3638,13 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, if(dh_key_blob->dwMagic == BCRYPT_DH_PRIVATE_MAGIC) { /* BCRYPT_DH_PRIVATE_BLOB additionally contains the Private data */ - dhctx->dh_privbn = _libssh2_wincng_bignum_init(); + dhctx->dh_privbn = ssh2_wcng_bn_init(); if(!dhctx->dh_privbn) { - wincng_safe_free(dh_key_blob, key_length_bytes); + wcng_safe_free(dh_key_blob, key_length_bytes); return -1; } - if(wincng_bignum_resize(dhctx->dh_privbn, dh_key_blob->cbKey)) { - wincng_safe_free(dh_key_blob, key_length_bytes); + if(wcng_bn_resize(dhctx->dh_privbn, dh_key_blob->cbKey)) { + wcng_safe_free(dh_key_blob, key_length_bytes); return -1; } @@ -3701,26 +3658,26 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, * odd primes can be used with the RSA-based fallback while * DH itself does not seem to care about it being odd or not. */ if(!(dhctx->dh_privbn->bignum[dhctx->dh_privbn->length - 1] % 2)) { - wincng_safe_free(dh_key_blob, key_length_bytes); + wcng_safe_free(dh_key_blob, key_length_bytes); /* discard everything first, then try again */ - _libssh2_dh_dtor(dhctx); - _libssh2_dh_init(dhctx); + ssh2_wcng_dh_dtor(dhctx); + ssh2_wcng_dh_init(dhctx); continue; } } - wincng_safe_free(dh_key_blob, key_length_bytes); + wcng_safe_free(dh_key_blob, key_length_bytes); return 0; } /* Generate x and e */ - dhctx->dh_privbn = _libssh2_wincng_bignum_init(); + dhctx->dh_privbn = ssh2_wcng_bn_init(); if(!dhctx->dh_privbn) return -1; - if(wincng_bignum_rand(dhctx->dh_privbn, (group_order * 8) - 1, 0, -1)) + if(wcng_bn_random(dhctx->dh_privbn, (group_order * 8) - 1, 0, -1)) return -1; - if(wincng_bignum_mod_exp(public, g, dhctx->dh_privbn, p)) + if(wcng_bn_mod_exp(public, g, dhctx->dh_privbn, p)) return -1; return 0; @@ -3730,10 +3687,10 @@ int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, * `*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 _libssh2_dh_secret(struct wincng_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p) +int ssh2_wcng_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p) { - if(wincng_ctx.hAlgDH && wincng_ctx.hasAlgDHwithKDF != -1 && + if(ssh2_wcng.hAlgDH && ssh2_wcng.hasAlgDHwithKDF != -1 && dhctx->dh_handle && dhctx->dh_params && f) { BCRYPT_KEY_HANDLE peer_public = NULL; BCRYPT_SECRET_HANDLE agreement = NULL; @@ -3763,19 +3720,21 @@ int _libssh2_dh_secret(struct wincng_dh_ctx *dhctx, libssh2_bn *secret, src = (unsigned char *)(dhctx->dh_params + 1); /* Modulus (the p-value from the first call) */ - memcpy_with_be_padding(dest, key_length_bytes, src, - dhctx->dh_params->cbKeyLength); + wcng_memcpy_with_be_padding(dest, key_length_bytes, + src, dhctx->dh_params->cbKeyLength); /* Generator (the g-value from the first call) */ - memcpy_with_be_padding(dest + key_length_bytes, key_length_bytes, - src + dhctx->dh_params->cbKeyLength, - dhctx->dh_params->cbKeyLength); + wcng_memcpy_with_be_padding(dest + key_length_bytes, + key_length_bytes, + src + dhctx->dh_params->cbKeyLength, + dhctx->dh_params->cbKeyLength); /* Public from the peer */ - memcpy_with_be_padding(dest + 2 * key_length_bytes, - key_length_bytes, f->bignum, f->length); + wcng_memcpy_with_be_padding(dest + 2 * key_length_bytes, + key_length_bytes, + f->bignum, f->length); } /* Import the peer public key information */ - status = BCryptImportKeyPair(wincng_ctx.hAlgDH, NULL, + status = BCryptImportKeyPair(ssh2_wcng.hAlgDH, NULL, BCRYPT_DH_PUBLIC_BLOB, &peer_public, (PUCHAR)public_blob, public_blob_len, 0); if(!BCRYPT_SUCCESS(status)) { @@ -3796,14 +3755,14 @@ int _libssh2_dh_secret(struct wincng_dh_ctx *dhctx, libssh2_bn *secret, 0, &secret_len_bytes, 0); if(!BCRYPT_SUCCESS(status)) { if(status == STATUS_NOT_SUPPORTED) { - wincng_ctx.hasAlgDHwithKDF = -1; + ssh2_wcng.hasAlgDHwithKDF = -1; } goto out; } /* Expand the secret bignum to be ready to receive the derived secret * */ - if(wincng_bignum_resize(secret, secret_len_bytes)) { + if(wcng_bn_resize(secret, secret_len_bytes)) { status = (NTSTATUS)STATUS_NO_MEMORY; goto out; } @@ -3814,7 +3773,7 @@ int _libssh2_dh_secret(struct wincng_dh_ctx *dhctx, libssh2_bn *secret, &secret_len_bytes, 0); if(!BCRYPT_SUCCESS(status)) { if(status == STATUS_NOT_SUPPORTED) { - wincng_ctx.hasAlgDHwithKDF = -1; + ssh2_wcng.hasAlgDHwithKDF = -1; } goto out; } @@ -3833,7 +3792,7 @@ int _libssh2_dh_secret(struct wincng_dh_ctx *dhctx, libssh2_bn *secret, } status = 0; - wincng_ctx.hasAlgDHwithKDF = 1; + ssh2_wcng.hasAlgDHwithKDF = 1; out: if(peer_public) { @@ -3845,8 +3804,7 @@ out: free(public_blob); - if(status == STATUS_NOT_SUPPORTED && - wincng_ctx.hasAlgDHwithKDF == -1) { + if(status == STATUS_NOT_SUPPORTED && ssh2_wcng.hasAlgDHwithKDF == -1) { goto fb; /* fallback to RSA-based implementation */ } return BCRYPT_SUCCESS(status) ? 0 : -1; @@ -3854,21 +3812,21 @@ out: fb: /* Compute the shared secret */ - return wincng_bignum_mod_exp(secret, f, dhctx->dh_privbn, p); + return wcng_bn_mod_exp(secret, f, dhctx->dh_privbn, p); } /* * Return supported key hash algo upgrades, see crypto.h */ -const char *_libssh2_supported_key_sign_algorithms(LIBSSH2_SESSION *session, - unsigned char *key_method, - size_t key_method_len) +const char *ssh2_supported_key_sign_algs(LIBSSH2_SESSION *session, + unsigned char *key_method, + size_t key_method_len) { (void)session; #if LIBSSH2_RSA_SHA2 if(key_method_len == 7 && - memcmp(key_method, "ssh-rsa", key_method_len) == 0) { + memcmp(key_method, "ssh-rsa", key_method_len) == 0) { return "rsa-sha2-512,rsa-sha2-256" #if LIBSSH2_RSA_SHA1 ",ssh-rsa" diff --git a/src/wincng.h b/src/wincng.h index 5fa28b8f..d79fa270 100644 --- a/src/wincng.h +++ b/src/wincng.h @@ -40,7 +40,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ -#define LIBSSH2_CRYPTO_ENGINE libssh2_wincng +#define SSH2_CRYPTO_ENGINE libssh2_wincng /* required for cross-compilation against the w64 mingw-runtime package */ #if defined(_WIN32_WINNT) && (_WIN32_WINNT < 0x0600) @@ -108,7 +108,7 @@ #if LIBSSH2_ECDSA #else -#define libssh2_ec_key void +#define ssh2_ec_key void #endif /*******************************************************************/ @@ -116,7 +116,7 @@ * Windows CNG backend: Global context handles */ -struct libssh2_wincng_ctx { +struct wcng_ctx { BCRYPT_ALG_HANDLE hAlgRNG; BCRYPT_ALG_HANDLE hAlgHashMD5; BCRYPT_ALG_HANDLE hAlgHashSHA1; @@ -137,32 +137,32 @@ struct libssh2_wincng_ctx { BCRYPT_ALG_HANDLE hAlgDH; BCRYPT_ALG_HANDLE hAlgChacha20; #if LIBSSH2_ECDSA - BCRYPT_ALG_HANDLE hAlgECDH[3]; /* indexed by libssh2_curve_type */ - BCRYPT_ALG_HANDLE hAlgECDSA[3]; /* indexed by libssh2_curve_type */ + BCRYPT_ALG_HANDLE hAlgECDH[3]; /* indexed by ssh2_curve_type */ + BCRYPT_ALG_HANDLE hAlgECDSA[3]; /* indexed by ssh2_curve_type */ #endif volatile int hasAlgDHwithKDF; /* -1=no, 0=maybe, 1=yes */ }; -extern struct libssh2_wincng_ctx wincng_ctx; +extern struct wcng_ctx ssh2_wcng; /*******************************************************************/ /* * Windows CNG backend: Generic functions */ -#define libssh2_crypto_init() _libssh2_wincng_init() -#define libssh2_crypto_exit() _libssh2_wincng_free() +#define ssh2_crypto_init() ssh2_wcng_init() +#define ssh2_crypto_exit() ssh2_wcng_free() -#define _libssh2_random(buf, len) _libssh2_wincng_random(buf, len) +#define ssh2_random(buf, len) ssh2_wcng_random(buf, len) -#define libssh2_prepare_iovec(vec, len) /* Empty. */ +#define ssh2_prepare_iovec(vec, len) /* Empty. */ /*******************************************************************/ /* * Windows CNG backend: Hash structure */ -struct wincng_hash_ctx { +struct wcng_hash_ctx { BCRYPT_HASH_HANDLE hHash; unsigned char *pbHashObject; ULONG dwHashObject; @@ -173,77 +173,77 @@ struct wincng_hash_ctx { * Windows CNG backend: Hash functions */ -#define libssh2_sha1_ctx struct wincng_hash_ctx -#define libssh2_sha1_init(ctx) \ - (_libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHashSHA1, \ - SHA_DIGEST_LENGTH, NULL, 0) == 0) -#define libssh2_sha1_update(ctx, data, datalen) \ - (_libssh2_wincng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) -#define libssh2_sha1_final(ctx, hash) \ - (_libssh2_wincng_hash_final(&(ctx), hash) == 0) -#define libssh2_sha1(data, datalen, hash) \ - _libssh2_wincng_hash(data, datalen, wincng_ctx.hAlgHashSHA1, \ - hash, SHA_DIGEST_LENGTH) +#define ssh2_sha1_ctx struct wcng_hash_ctx +#define ssh2_sha1_init(ctx) \ + (ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHashSHA1, \ + SHA_DIGEST_LENGTH, NULL, 0) == 0) +#define ssh2_sha1_update(ctx, data, datalen) \ + (ssh2_wcng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) +#define ssh2_sha1_final(ctx, hash) \ + (ssh2_wcng_hash_final(&(ctx), hash) == 0) +#define ssh2_sha1(data, datalen, hash) \ + ssh2_wcng_hash(data, datalen, ssh2_wcng.hAlgHashSHA1, \ + hash, SHA_DIGEST_LENGTH) -#define libssh2_sha256_ctx struct wincng_hash_ctx -#define libssh2_sha256_init(ctx) \ - (_libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHashSHA256, \ - SHA256_DIGEST_LENGTH, NULL, 0) == 0) -#define libssh2_sha256_update(ctx, data, datalen) \ - (_libssh2_wincng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) -#define libssh2_sha256_final(ctx, hash) \ - (_libssh2_wincng_hash_final(&(ctx), hash) == 0) -#define libssh2_sha256(data, datalen, hash) \ - _libssh2_wincng_hash(data, datalen, wincng_ctx.hAlgHashSHA256, \ - hash, SHA256_DIGEST_LENGTH) +#define ssh2_sha256_ctx struct wcng_hash_ctx +#define ssh2_sha256_init(ctx) \ + (ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHashSHA256, \ + SHA256_DIGEST_LENGTH, NULL, 0) == 0) +#define ssh2_sha256_update(ctx, data, datalen) \ + (ssh2_wcng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) +#define ssh2_sha256_final(ctx, hash) \ + (ssh2_wcng_hash_final(&(ctx), hash) == 0) +#define ssh2_sha256(data, datalen, hash) \ + ssh2_wcng_hash(data, datalen, ssh2_wcng.hAlgHashSHA256, \ + hash, SHA256_DIGEST_LENGTH) -#define libssh2_sha384_ctx struct wincng_hash_ctx -#define libssh2_sha384_init(ctx) \ - (_libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHashSHA384, \ - SHA384_DIGEST_LENGTH, NULL, 0) == 0) -#define libssh2_sha384_update(ctx, data, datalen) \ - (_libssh2_wincng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) -#define libssh2_sha384_final(ctx, hash) \ - (_libssh2_wincng_hash_final(&(ctx), hash) == 0) -#define libssh2_sha384(data, datalen, hash) \ - _libssh2_wincng_hash(data, datalen, wincng_ctx.hAlgHashSHA384, \ - hash, SHA384_DIGEST_LENGTH) +#define ssh2_sha384_ctx struct wcng_hash_ctx +#define ssh2_sha384_init(ctx) \ + (ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHashSHA384, \ + SHA384_DIGEST_LENGTH, NULL, 0) == 0) +#define ssh2_sha384_update(ctx, data, datalen) \ + (ssh2_wcng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) +#define ssh2_sha384_final(ctx, hash) \ + (ssh2_wcng_hash_final(&(ctx), hash) == 0) +#define ssh2_sha384(data, datalen, hash) \ + ssh2_wcng_hash(data, datalen, ssh2_wcng.hAlgHashSHA384, \ + hash, SHA384_DIGEST_LENGTH) -#define libssh2_sha512_ctx struct wincng_hash_ctx -#define libssh2_sha512_init(ctx) \ - (_libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHashSHA512, \ - SHA512_DIGEST_LENGTH, NULL, 0) == 0) -#define libssh2_sha512_update(ctx, data, datalen) \ - (_libssh2_wincng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) -#define libssh2_sha512_final(ctx, hash) \ - (_libssh2_wincng_hash_final(&(ctx), hash) == 0) -#define libssh2_sha512(data, datalen, hash) \ - _libssh2_wincng_hash(data, datalen, wincng_ctx.hAlgHashSHA512, \ - hash, SHA512_DIGEST_LENGTH) +#define ssh2_sha512_ctx struct wcng_hash_ctx +#define ssh2_sha512_init(ctx) \ + (ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHashSHA512, \ + SHA512_DIGEST_LENGTH, NULL, 0) == 0) +#define ssh2_sha512_update(ctx, data, datalen) \ + (ssh2_wcng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) +#define ssh2_sha512_final(ctx, hash) \ + (ssh2_wcng_hash_final(&(ctx), hash) == 0) +#define ssh2_sha512(data, datalen, hash) \ + ssh2_wcng_hash(data, datalen, ssh2_wcng.hAlgHashSHA512, \ + hash, SHA512_DIGEST_LENGTH) #if LIBSSH2_MD5 || LIBSSH2_MD5_PEM -#define libssh2_md5_ctx struct wincng_hash_ctx -#define libssh2_md5_init(ctx) \ - (_libssh2_wincng_hash_init(ctx, wincng_ctx.hAlgHashMD5, \ - MD5_DIGEST_LENGTH, NULL, 0) == 0) -#define libssh2_md5_update(ctx, data, datalen) \ - (_libssh2_wincng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) -#define libssh2_md5_final(ctx, hash) \ - (_libssh2_wincng_hash_final(&(ctx), hash) == 0) +#define ssh2_md5_ctx struct wcng_hash_ctx +#define ssh2_md5_init(ctx) \ + (ssh2_wcng_hash_init(ctx, ssh2_wcng.hAlgHashMD5, \ + MD5_DIGEST_LENGTH, NULL, 0) == 0) +#define ssh2_md5_update(ctx, data, datalen) \ + (ssh2_wcng_hash_update(&(ctx), data, (ULONG)(datalen)) == 0) +#define ssh2_md5_final(ctx, hash) \ + (ssh2_wcng_hash_final(&(ctx), hash) == 0) #endif /* * Windows CNG backend: HMAC functions */ -#define libssh2_hmac_ctx struct wincng_hash_ctx +#define ssh2_hmac_ctx struct wcng_hash_ctx /*******************************************************************/ /* * Windows CNG backend: Key Context structure */ -struct wincng_key_ctx { +struct wcng_key_ctx { BCRYPT_KEY_HANDLE hKey; void *pbKeyObject; DWORD cbKeyObject; @@ -253,127 +253,112 @@ struct wincng_key_ctx { * Windows CNG backend: RSA functions */ -#define libssh2_rsa_ctx struct wincng_key_ctx -#define _libssh2_rsa_new(rsactx, e, e_len, n, n_len, \ - d, d_len, p, p_len, q, q_len, \ - e1, e1_len, e2, e2_len, c, c_len) \ - _libssh2_wincng_rsa_new(rsactx, e, e_len, n, n_len, \ - d, d_len, p, p_len, q, q_len, \ - e1, e1_len, e2, e2_len, c, c_len) -#define _libssh2_rsa_new_private(rsactx, s, filename, passphrase) \ - _libssh2_wincng_rsa_new_private(rsactx, s, filename, passphrase) -#define _libssh2_rsa_new_private_frommemory(rsactx, s, filedata, \ - filedata_len, passphrase) \ - _libssh2_wincng_rsa_new_private_frommemory(rsactx, s, filedata, \ - filedata_len, passphrase) -#define _libssh2_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) \ - _libssh2_wincng_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) -#define _libssh2_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) \ - _libssh2_wincng_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) -#define _libssh2_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) \ - _libssh2_wincng_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) -#define _libssh2_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) \ - _libssh2_wincng_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) -#define _libssh2_rsa_free(rsactx) \ - _libssh2_wincng_rsa_free(rsactx) +#define ssh2_rsa_ctx struct wcng_key_ctx +#define ssh2_rsa_new(rsactx, e, e_len, n, n_len, d, d_len, p, p_len, \ + q, q_len, e1, e1_len, e2, e2_len, c, c_len) \ + ssh2_wcng_rsa_new(rsactx, e, e_len, n, n_len, d, d_len, p, p_len, \ + q, q_len, e1, e1_len, e2, e2_len, c, c_len) +#define ssh2_rsa_new_private(rsactx, s, filename, passphrase) \ + ssh2_wcng_rsa_new_private(rsactx, s, filename, passphrase) +#define ssh2_rsa_new_private_frommemory(rsactx, s, filedata, \ + filedata_len, passphrase) \ + ssh2_wcng_rsa_new_private_frommemory(rsactx, s, filedata, \ + filedata_len, passphrase) +#define ssh2_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) \ + ssh2_wcng_rsa_sha1_sign(s, rsactx, hash, hash_len, sig, sig_len) +#define ssh2_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) \ + ssh2_wcng_rsa_sha2_sign(s, rsactx, hash, hash_len, sig, sig_len) +#define ssh2_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) \ + ssh2_wcng_rsa_sha1_verify(rsactx, sig, sig_len, m, m_len) +#define ssh2_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) \ + ssh2_wcng_rsa_sha2_verify(rsactx, hash_len, sig, sig_len, m, m_len) +#define ssh2_rsa_free(rsactx) \ + ssh2_wcng_rsa_free(rsactx) /* * Windows CNG backend: DSA functions */ -#define libssh2_dsa_ctx struct wincng_key_ctx -#define _libssh2_dsa_new(dsactx, p, p_len, q, q_len, \ - g, g_len, y, y_len, x, x_len) \ - _libssh2_wincng_dsa_new(dsactx, p, p_len, q, q_len, \ - g, g_len, y, y_len, x, x_len) -#define _libssh2_dsa_new_private(dsactx, s, filename, passphrase) \ - _libssh2_wincng_dsa_new_private(dsactx, s, filename, passphrase) -#define _libssh2_dsa_new_private_frommemory(dsactx, s, filedata, \ - filedata_len, passphrase) \ - _libssh2_wincng_dsa_new_private_frommemory(dsactx, s, filedata, \ - filedata_len, passphrase) -#define _libssh2_dsa_sha1_sign(dsactx, hash, hash_len, sig) \ - _libssh2_wincng_dsa_sha1_sign(dsactx, hash, hash_len, sig) -#define _libssh2_dsa_sha1_verify(dsactx, sig, m, m_len) \ - _libssh2_wincng_dsa_sha1_verify(dsactx, sig, m, m_len) -#define _libssh2_dsa_free(dsactx) \ - _libssh2_wincng_dsa_free(dsactx) +#define ssh2_dsa_ctx struct wcng_key_ctx +#define ssh2_dsa_new(dsactx, p, p_len, q, q_len, g, g_len, y, y_len, \ + x, x_len) \ + ssh2_wcng_dsa_new(dsactx, p, p_len, q, q_len, g, g_len, y, y_len, \ + x, x_len) +#define ssh2_dsa_new_private(dsactx, s, filename, passphrase) \ + ssh2_wcng_dsa_new_private(dsactx, s, filename, passphrase) +#define ssh2_dsa_new_private_frommemory(dsactx, s, filedata, \ + filedata_len, passphrase) \ + ssh2_wcng_dsa_new_private_frommemory(dsactx, s, filedata, \ + filedata_len, passphrase) +#define ssh2_dsa_sha1_sign(dsactx, hash, hash_len, sig) \ + ssh2_wcng_dsa_sha1_sign(dsactx, hash, hash_len, sig) +#define ssh2_dsa_sha1_verify(dsactx, sig, m, m_len) \ + ssh2_wcng_dsa_sha1_verify(dsactx, sig, m, m_len) +#define ssh2_dsa_free(dsactx) \ + ssh2_wcng_dsa_free(dsactx) /* * Windows CNG backend: ECDSA functions */ typedef enum { - LIBSSH2_EC_CURVE_NISTP256 = 0, - LIBSSH2_EC_CURVE_NISTP384 = 1, - LIBSSH2_EC_CURVE_NISTP521 = 2, -} libssh2_curve_type; + SSH2_EC_CURVE_NISTP256 = 0, + SSH2_EC_CURVE_NISTP384 = 1, + SSH2_EC_CURVE_NISTP521 = 2, +} ssh2_curve_type; -struct wincng_ecdsa_ctx { +struct wcng_ecdsa_ctx { BCRYPT_KEY_HANDLE handle; - libssh2_curve_type curve; + ssh2_curve_type curve; }; -#define libssh2_ecdsa_ctx struct wincng_ecdsa_ctx - +#define ssh2_ecdsa_ctx struct wcng_ecdsa_ctx #if LIBSSH2_ECDSA -#define libssh2_ec_key struct wincng_ecdsa_ctx +#define ssh2_ec_key struct wcng_ecdsa_ctx #endif - -void _libssh2_wincng_ecdsa_free(libssh2_ecdsa_ctx* ctx); - -#define _libssh2_ecdsa_create_key(session, privkey, pubkey_octal, \ - pubkey_octal_len, curve) \ - _libssh2_wincng_ecdh_create_key(session, privkey, pubkey_octal, \ - pubkey_octal_len, curve) - -#define _libssh2_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) \ - _libssh2_wincng_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) - -#define _libssh2_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) \ - _libssh2_wincng_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) - -#define _libssh2_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) \ - _libssh2_wincng_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) - -#define _libssh2_ecdsa_new_private(ctx, session, filename, passphrase) \ - _libssh2_wincng_ecdsa_new_private(ctx, session, filename, passphrase) - -#define _libssh2_ecdsa_new_private_frommemory(ctx, session, filedata, \ - filedata_len, passphrase) \ - _libssh2_wincng_ecdsa_new_private_frommemory(ctx, session, filedata, \ - filedata_len, passphrase) - -#define _libssh2_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) \ - _libssh2_wincng_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) - -#define _libssh2_ecdsa_get_curve_type(ctx) \ - _libssh2_wincng_ecdsa_get_curve_type(ctx) - -#define _libssh2_ecdsa_free(ecdsactx) \ - _libssh2_wincng_ecdsa_free(ecdsactx) +void ssh2_wcng_ecdsa_free(ssh2_ecdsa_ctx* ctx); +#define ssh2_ecdsa_create_key(session, privkey, pubkey_octal, \ + pubkey_octal_len, curve) \ + ssh2_wcng_ecdh_create_key(session, privkey, pubkey_octal, \ + pubkey_octal_len, curve) +#define ssh2_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) \ + ssh2_wcng_ecdsa_curve_name_with_octal_new(ctx, k, k_len, curve) +#define ssh2_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) \ + ssh2_wcng_ecdh_gen_k(k, privkey, server_pubkey, server_pubkey_len) +#define ssh2_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) \ + ssh2_wcng_ecdsa_verify(ctx, r, r_len, s, s_len, m, m_len) +#define ssh2_ecdsa_new_private(ctx, session, filename, passphrase) \ + ssh2_wcng_ecdsa_new_private(ctx, session, filename, passphrase) +#define ssh2_ecdsa_new_private_frommemory(ctx, session, filedata, \ + filedata_len, passphrase) \ + ssh2_wcng_ecdsa_new_private_frommemory(ctx, session, filedata, \ + filedata_len, passphrase) +#define ssh2_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) \ + ssh2_wcng_ecdsa_sign(session, ctx, hash, hash_len, sign, sign_len) +#define ssh2_ecdsa_get_curve_type(ctx) \ + ssh2_wcng_ecdsa_get_curve_type(ctx) +#define ssh2_ecdsa_free(ecdsactx) \ + ssh2_wcng_ecdsa_free(ecdsactx) /* * Windows CNG backend: Key functions */ -#define _libssh2_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) \ - _libssh2_wincng_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) -#define _libssh2_pub_priv_keyfilememory(s, m, m_len, p, p_len, \ - pk, pk_len, pw) \ - _libssh2_wincng_pub_priv_keyfilememory(s, m, m_len, p, p_len, \ - pk, pk_len, pw) -#define _libssh2_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ - f, kh, kh_len, pk, pk_len, pw) \ - _libssh2_wincng_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ - f, kh, kh_len, pk, pk_len, pw) +#define ssh2_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) \ + ssh2_wcng_pub_priv_keyfile(s, m, m_len, p, p_len, pk, pw) +#define ssh2_pub_priv_keyfilememory(s, m, m_len, p, p_len, pk, pk_len, pw) \ + ssh2_wcng_pub_priv_keyfilememory(s, m, m_len, p, p_len, pk, pk_len, pw) +#define ssh2_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ + f, kh, kh_len, pk, pk_len, pw) \ + ssh2_wcng_sk_pub_keyfilememory(s, m, m_len, p, p_len, alg, app, \ + f, kh, kh_len, pk, pk_len, pw) /*******************************************************************/ /* * Windows CNG backend: Cipher Context structure */ -struct libssh2_wincng_cipher_ctx { +struct wcng_cipher_ctx { BCRYPT_KEY_HANDLE hKey; unsigned char *pbKeyObject; unsigned char *pbIV; @@ -384,13 +369,13 @@ struct libssh2_wincng_cipher_ctx { ULONG dwCtrLength; }; -#define libssh2_cipher_ctx struct libssh2_wincng_cipher_ctx +#define ssh2_cipher_ctx struct wcng_cipher_ctx /* * Windows CNG backend: Cipher Type structure */ -struct libssh2_wincng_cipher_type { +struct wcng_cipher_t { BCRYPT_ALG_HANDLE *phAlg; ULONG dwKeyLength; int useIV; /* TODO: Convert to bool when a C89-compatible bool type @@ -398,69 +383,69 @@ struct libssh2_wincng_cipher_type { int ctrMode; }; -#define LIBSSH2_CIPHER_T(type) struct libssh2_wincng_cipher_type type +#define SSH2_CIPHER_T(type) struct wcng_cipher_t type -#define libssh2_cipher_aes256ctr { &wincng_ctx.hAlgAES_ECB, 32, 0, 1 } -#define libssh2_cipher_aes192ctr { &wincng_ctx.hAlgAES_ECB, 24, 0, 1 } -#define libssh2_cipher_aes128ctr { &wincng_ctx.hAlgAES_ECB, 16, 0, 1 } -#define libssh2_cipher_aes256 { &wincng_ctx.hAlgAES_CBC, 32, 1, 0 } -#define libssh2_cipher_aes192 { &wincng_ctx.hAlgAES_CBC, 24, 1, 0 } -#define libssh2_cipher_aes128 { &wincng_ctx.hAlgAES_CBC, 16, 1, 0 } -#define libssh2_cipher_arcfour { &wincng_ctx.hAlgRC4_NA, 16, 0, 0 } -#define libssh2_cipher_3des { &wincng_ctx.hAlg3DES_CBC, 24, 1, 0 } -#define libssh2_cipher_chacha20 { &wincng_ctx.hAlgChacha20, 24, 1, 0 } +#define ssh2_cipher_aes256ctr { &ssh2_wcng.hAlgAES_ECB, 32, 0, 1 } +#define ssh2_cipher_aes192ctr { &ssh2_wcng.hAlgAES_ECB, 24, 0, 1 } +#define ssh2_cipher_aes128ctr { &ssh2_wcng.hAlgAES_ECB, 16, 0, 1 } +#define ssh2_cipher_aes256 { &ssh2_wcng.hAlgAES_CBC, 32, 1, 0 } +#define ssh2_cipher_aes192 { &ssh2_wcng.hAlgAES_CBC, 24, 1, 0 } +#define ssh2_cipher_aes128 { &ssh2_wcng.hAlgAES_CBC, 16, 1, 0 } +#define ssh2_cipher_arcfour { &ssh2_wcng.hAlgRC4_NA, 16, 0, 0 } +#define ssh2_cipher_3des { &ssh2_wcng.hAlg3DES_CBC, 24, 1, 0 } +#define ssh2_cipher_chacha20 { &ssh2_wcng.hAlgChacha20, 24, 1, 0 } /* * Windows CNG backend: Cipher functions */ -#define _libssh2_cipher_init(ctx, type, iv, secret, encrypt) \ - _libssh2_wincng_cipher_init(ctx, type, iv, secret, encrypt) -#define _libssh2_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) \ - _libssh2_wincng_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) -#define _libssh2_cipher_dtor(ctx) \ - _libssh2_wincng_cipher_dtor(ctx) +#define ssh2_cipher_init(ctx, type, iv, secret, encrypt) \ + ssh2_wcng_cipher_init(ctx, type, iv, secret, encrypt) +#define ssh2_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) \ + ssh2_wcng_cipher_crypt(ctx, type, encrypt, block, blocklen, fl) +#define ssh2_cipher_dtor(ctx) \ + ssh2_wcng_cipher_dtor(ctx) /*******************************************************************/ /* * Windows CNG backend: BigNumber Context */ -#define libssh2_bn_ctx int /* not used */ -#define _libssh2_bn_ctx_new() 0 /* not used */ -#define _libssh2_bn_ctx_free(bnctx) ((void)0) /* not used */ +#define ssh2_bn_ctx int /* not used */ +#define ssh2_bn_ctx_new() 0 /* not used */ +#define ssh2_bn_ctx_free(bnctx) ((void)0) /* not used */ /*******************************************************************/ /* * Windows CNG backend: BigNumber structure */ -struct libssh2_wincng_bignum { +struct wcng_bn { unsigned char *bignum; ULONG length; }; -#define libssh2_bn struct libssh2_wincng_bignum +#define ssh2_bn struct wcng_bn /* * Windows CNG backend: BigNumber functions */ -#define _libssh2_bn_init() \ - _libssh2_wincng_bignum_init() -#define _libssh2_bn_init_from_bin() \ - _libssh2_bn_init() -#define _libssh2_bn_set_word(bn, word) \ - _libssh2_wincng_bignum_set_word(bn, word) -#define _libssh2_bn_from_bin(bn, len, bin) \ - _libssh2_wincng_bignum_from_bin(bn, (ULONG)(len), bin) -#define _libssh2_bn_to_bin(bn, bin) \ - _libssh2_wincng_bignum_to_bin(bn, bin) -#define _libssh2_bn_bytes(bn) bn->length -#define _libssh2_bn_bits(bn) \ - _libssh2_wincng_bignum_bits(bn) -#define _libssh2_bn_free(bn) \ - _libssh2_wincng_bignum_free(bn) +#define ssh2_bn_init() \ + ssh2_wcng_bn_init() +#define ssh2_bn_init_from_bin() \ + ssh2_bn_init() +#define ssh2_bn_set_word(bn, word) \ + ssh2_wcng_bn_set_word(bn, word) +#define ssh2_bn_from_bin(bn, len, bin) \ + ssh2_wcng_bn_from_bin(bn, (ULONG)(len), bin) +#define ssh2_bn_to_bin(bn, bin) \ + ssh2_wcng_bn_to_bin(bn, bin) +#define ssh2_bn_bytes(bn) bn->length +#define ssh2_bn_bits(bn) \ + ssh2_wcng_bn_bits(bn) +#define ssh2_bn_free(bn) \ + ssh2_wcng_bn_free(bn) /* * Windows CNG backend: Diffie-Hellman support @@ -468,13 +453,13 @@ struct libssh2_wincng_bignum { /* Default generate and safe prime sizes for diffie-hellman-group-exchange-sha1 */ -#define LIBSSH2_DH_GEX_MINGROUP 2048 -#define LIBSSH2_DH_GEX_OPTGROUP 4096 -#define LIBSSH2_DH_GEX_MAXGROUP 4096 +#define SSH2_DH_GEX_MINGROUP 2048 +#define SSH2_DH_GEX_OPTGROUP 4096 +#define SSH2_DH_GEX_MAXGROUP 4096 -#define LIBSSH2_DH_MAX_MODULUS_BITS 16384 +#define SSH2_DH_MAX_MODULUS_BITS 16384 -struct wincng_dh_ctx { +struct wcng_dh_ctx { /* holds our private and public key components */ BCRYPT_KEY_HANDLE dh_handle; /* records the parsed out modulus and generator @@ -482,57 +467,56 @@ struct wincng_dh_ctx { BCRYPT_DH_PARAMETER_HEADER *dh_params; /* records the parsed out private key component for * fallback if the DH API raw KDF is not supported */ - struct libssh2_wincng_bignum *dh_privbn; + struct wcng_bn *dh_privbn; }; -#define libssh2_dh_ctx struct wincng_dh_ctx +#define ssh2_dh_ctx struct wcng_dh_ctx -#define libssh2_dh_init(dhctx) _libssh2_dh_init(dhctx) -#define libssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ - _libssh2_dh_key_pair(dhctx, public, g, p, group_order) -#define libssh2_dh_secret(dhctx, secret, f, p, bnctx) \ - _libssh2_dh_secret(dhctx, secret, f, p) -#define libssh2_dh_dtor(dhctx) _libssh2_dh_dtor(dhctx) +#define ssh2_dh_init(dhctx) ssh2_wcng_dh_init(dhctx) +#define ssh2_dh_key_pair(dhctx, public, g, p, group_order, bnctx) \ + ssh2_wcng_dh_key_pair(dhctx, public, g, p, group_order) +#define ssh2_dh_secret(dhctx, secret, f, p, bnctx) \ + ssh2_wcng_dh_secret(dhctx, secret, f, p) +#define ssh2_dh_dtor(dhctx) ssh2_wcng_dh_dtor(dhctx) /*******************************************************************/ /* * Windows CNG backend: forward declarations */ -void _libssh2_wincng_init(void); -void _libssh2_wincng_free(void); -int _libssh2_wincng_random(void *buf, size_t len); +void ssh2_wcng_init(void); +void ssh2_wcng_free(void); +int ssh2_wcng_random(void *buf, size_t len); -int _libssh2_wincng_hash_init(struct wincng_hash_ctx *ctx, - BCRYPT_ALG_HANDLE hAlg, ULONG hashlen, - unsigned char *key, ULONG keylen); -int _libssh2_wincng_hash_update(struct wincng_hash_ctx *ctx, - const void *data, ULONG datalen); -int _libssh2_wincng_hash_final(struct wincng_hash_ctx *ctx, - unsigned char *hash); -int _libssh2_wincng_hash(const unsigned char *data, ULONG datalen, - BCRYPT_ALG_HANDLE hAlg, - unsigned char *hash, ULONG hashlen); +int ssh2_wcng_hash_init(struct wcng_hash_ctx *ctx, + BCRYPT_ALG_HANDLE hAlg, ULONG hashlen, + unsigned char *key, ULONG keylen); +int ssh2_wcng_hash_update(struct wcng_hash_ctx *ctx, + const void *data, ULONG datalen); +int ssh2_wcng_hash_final(struct wcng_hash_ctx *ctx, + unsigned char *hash); +int ssh2_wcng_hash(const unsigned char *data, ULONG datalen, + BCRYPT_ALG_HANDLE hAlg, + unsigned char *hash, ULONG hashlen); -void _libssh2_wincng_rsa_free(libssh2_rsa_ctx *rsa); +void ssh2_wcng_rsa_free(ssh2_rsa_ctx *rsa); #if LIBSSH2_DSA -void _libssh2_wincng_dsa_free(libssh2_dsa_ctx *dsa); +void ssh2_wcng_dsa_free(ssh2_dsa_ctx *dsa); #endif -void _libssh2_wincng_cipher_dtor(libssh2_cipher_ctx *ctx); +void ssh2_wcng_cipher_dtor(ssh2_cipher_ctx *ctx); -libssh2_bn *_libssh2_wincng_bignum_init(void); -int _libssh2_wincng_bignum_set_word(libssh2_bn *bn, ULONG word); -ULONG _libssh2_wincng_bignum_bits(const libssh2_bn *bn); -int _libssh2_wincng_bignum_from_bin(libssh2_bn *bn, ULONG len, - const unsigned char *bin); -int _libssh2_wincng_bignum_to_bin(const libssh2_bn *bn, unsigned char *bin); -void _libssh2_wincng_bignum_free(libssh2_bn *bn); -void _libssh2_dh_init(struct wincng_dh_ctx *dhctx); -int _libssh2_dh_key_pair(struct wincng_dh_ctx *dhctx, libssh2_bn *public, - libssh2_bn *g, libssh2_bn *p, int group_order); -int _libssh2_dh_secret(struct wincng_dh_ctx *dhctx, libssh2_bn *secret, - libssh2_bn *f, libssh2_bn *p); -void _libssh2_dh_dtor(struct wincng_dh_ctx *dhctx); +ssh2_bn *ssh2_wcng_bn_init(void); +int ssh2_wcng_bn_set_word(ssh2_bn *bn, ULONG word); +ULONG ssh2_wcng_bn_bits(const ssh2_bn *bn); +int ssh2_wcng_bn_from_bin(ssh2_bn *bn, ULONG len, const unsigned char *bin); +int ssh2_wcng_bn_to_bin(const ssh2_bn *bn, unsigned char *bin); +void ssh2_wcng_bn_free(ssh2_bn *bn); +void ssh2_wcng_dh_init(ssh2_dh_ctx *dhctx); +int ssh2_wcng_dh_key_pair(ssh2_dh_ctx *dhctx, ssh2_bn *public, + ssh2_bn *g, ssh2_bn *p, int group_order); +int ssh2_wcng_dh_secret(ssh2_dh_ctx *dhctx, ssh2_bn *secret, + ssh2_bn *f, ssh2_bn *p); +void ssh2_wcng_dh_dtor(ssh2_dh_ctx *dhctx); #endif /* LIBSSH2_WINCNG_H */ diff --git a/tests/openssh_fixture.c b/tests/openssh_fixture.c index 2837e76f..a56451e8 100644 --- a/tests/openssh_fixture.c +++ b/tests/openssh_fixture.c @@ -80,7 +80,7 @@ int openssh_fixture_have_docker(void) } static int run_command_varg(char **output, const char *command, va_list args) - LIBSSH2_PRINTF(2, 0); + SSH2_PRINTF(2, 0); static int run_command_varg(char **output, const char *command, va_list args) { @@ -155,7 +155,7 @@ static int run_command_varg(char **output, const char *command, va_list args) } static int run_command(char **output, const char *command, ...) - LIBSSH2_PRINTF(2, 3); + SSH2_PRINTF(2, 3); static int run_command(char **output, const char *command, ...) { diff --git a/tests/test_auth_keyboard_info_request.c b/tests/test_auth_keyboard_info_request.c index 5c885370..3c61f571 100644 --- a/tests/test_auth_keyboard_info_request.c +++ b/tests/test_auth_keyboard_info_request.c @@ -224,7 +224,6 @@ failed_malloc_test_cases[FAILED_MALLOC_TEST_CASES_LEN] = { static int alloc_count = 0; static int free_count = 0; -/* libssh2_default_alloc */ static LIBSSH2_ALLOC_FUNC(test_alloc) { int *threshold_int_ptr = *abstract; @@ -236,7 +235,6 @@ static LIBSSH2_ALLOC_FUNC(test_alloc) return malloc(count); } -/* libssh2_default_free */ static LIBSSH2_FREE_FUNC(test_free) { (void)abstract; @@ -262,7 +260,7 @@ static int test_case(int num, return 1; } - session->userauth_kybd_data = LIBSSH2_ALLOC(session, data_len); + session->userauth_kybd_data = SSH2_ALLOC(session, data_len); session->userauth_kybd_data_len = data_len; memcpy(session->userauth_kybd_data, data, data_len); diff --git a/tests/test_hostkey.c b/tests/test_hostkey.c index f54a275f..ad38bb0b 100644 --- a/tests/test_hostkey.c +++ b/tests/test_hostkey.c @@ -30,24 +30,24 @@ int test(LIBSSH2_SESSION *session) const char *hostkey = libssh2_session_hostkey(session, &len, &type); if(!hostkey) { - print_last_session_error("libssh2_session_hostkey"); + print_last_session_error("libssh2_session_hostkey()"); return 1; } if(type == LIBSSH2_HOSTKEY_TYPE_ED25519) { - rc = _libssh2_base64_decode(session, &expected_hostkey, &expected_len, - EXPECTED_ED25519_HOSTKEY, - strlen(EXPECTED_ED25519_HOSTKEY)); + rc = ssh2_base64_decode(session, &expected_hostkey, &expected_len, + EXPECTED_ED25519_HOSTKEY, + strlen(EXPECTED_ED25519_HOSTKEY)); } else if(type == LIBSSH2_HOSTKEY_TYPE_ECDSA_256) { - rc = _libssh2_base64_decode(session, &expected_hostkey, &expected_len, - EXPECTED_ECDSA_HOSTKEY, - strlen(EXPECTED_ECDSA_HOSTKEY)); + rc = ssh2_base64_decode(session, &expected_hostkey, &expected_len, + EXPECTED_ECDSA_HOSTKEY, + strlen(EXPECTED_ECDSA_HOSTKEY)); } else if(type == LIBSSH2_HOSTKEY_TYPE_RSA) { - rc = _libssh2_base64_decode(session, &expected_hostkey, &expected_len, - EXPECTED_RSA_HOSTKEY, - strlen(EXPECTED_RSA_HOSTKEY)); + rc = ssh2_base64_decode(session, &expected_hostkey, &expected_len, + EXPECTED_RSA_HOSTKEY, + strlen(EXPECTED_RSA_HOSTKEY)); } else { fprintf(stderr, "Unexpected type of hostkey: %i\n", type); @@ -55,7 +55,7 @@ int test(LIBSSH2_SESSION *session) } if(rc) { - print_last_session_error("_libssh2_base64_decode"); + print_last_session_error("ssh2_base64_decode()"); return 1; } diff --git a/tests/test_simple.c b/tests/test_simple.c index e9956f08..35dea30c 100644 --- a/tests/test_simple.c +++ b/tests/test_simple.c @@ -43,21 +43,19 @@ #include #include -static int test_libssh2_base64_decode(LIBSSH2_SESSION *session) +static int test_ssh2_base64_decode(LIBSSH2_SESSION *session) { char *data; size_t datalen; const char *src = "Zm5vcmQ="; int ret; - ret = _libssh2_base64_decode(session, &data, &datalen, - src, strlen(src)); + ret = ssh2_base64_decode(session, &data, &datalen, src, strlen(src)); if(ret) return ret; if(datalen != 5 || strcmp(data, "fnord") != 0) { - fprintf(stderr, - "_libssh2_base64_decode() failed (%d, %.*s)\n", + fprintf(stderr, "ssh2_base64_decode() failed (%d, %.*s)\n", (int)datalen, (int)datalen, data); return 1; } @@ -86,7 +84,7 @@ int main(int argc, char *argv[]) return 1; } - test_libssh2_base64_decode(session); + test_ssh2_base64_decode(session); libssh2_session_free(session);