Skip to content

Add support for [email protected] and… - #698

Merged
willco007 merged 15 commits into
libssh2:masterfrom
MichaelBuckley:sk-keys
Sep 29, 2022
Merged

willco007 merged 15 commits into
libssh2:masterfrom
MichaelBuckley:sk-keys

Conversation

@MichaelBuckley

Copy link
Copy Markdown
Contributor

… [email protected] key exchange. These are the FIDO keys. There are also certificate variants that are not covered as part of this pull request. I hope to add them in a future PR. They are described in https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.u2f

This change adds a new public function, libssh2_userauth_publickey_sk. This function takes a callback function that's responsible for performing fido signing and filling out a new LIBSSH2_SK_SIG_INFO struct. The flags field passed into this callback uses two new #defines from libssh2.h:

  • LIBSSH2_SK_PRESENCE_REQUIRED indicates whether the user must touch the security key to authenticate.
  • LIBSSH2_SK_VERIFICATION_REQUIRED indicates whether the user must supply a PIN to the security key. The PIN is not passed to the callback function. It is up to the callback function to prompt the user for the PIN, or the client can pass it as part of the abstract parameter. This was done to prevent the need to parse the key multiple times to determine if a PIN is required.

I am posting this PR to solicit feedback on this APi, as well as to ask for more eyes on the implementation to check for mistakes.

The only implementation provided is for the openssl backend, as that's the backend I'm using. Stubs are provided for the other backends.

I'm not sure how to best update documentation for the new public function. There a bunch of man pages in the docs directory, but they appear to be outdated, and some of them are incomplete. I'm not sure if they're actively maintained.

@MichaelBuckley

Copy link
Copy Markdown
Contributor Author

I'll fix the CI errors and push fixes tomorrow.

@MichaelBuckley

Copy link
Copy Markdown
Contributor Author

All CI tests now passing.

@willco007
willco007 merged commit ed439a2 into libssh2:master Sep 29, 2022
vszakats added a commit to vszakats/libssh2 that referenced this pull request Sep 13, 2025
@vszakats vszakats mentioned this pull request Sep 13, 2025
vszakats added a commit that referenced this pull request Sep 14, 2025
- userauth: fix NULL dereference when out-of-memory.
  Also fix indentation.
  Follow-up to 3a6ab70 #1314

- openssl: drop redundant NULL check and logic.
  Follow-up to ed439a2 #698

Pointed out by CodeQL

Closes #1656
vszakats added a commit that referenced this pull request Jun 3, 2026
Based on it having a public man page, it was likely meant to be a public
function. Mark it `LIBSSH2_API` to export it from the DLL on Windows.

Also move its public prototype next to userauth functions, since its
implemented in userauth.c (though not using the namespace).

Follow-up to ed439a2 #698

Closes #1965
vszakats added a commit that referenced this pull request Jun 4, 2026
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_<crypto-backend>_`: non-static crypto functions.
  where `<crypto-backend>` 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 689b202 #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: ed439a2 #698

Follow-up to 8c9b777 #1931
Follow-up to 689b202 #1923
Ref: #1949

Closes #1961
vszakats added a commit that referenced this pull request Jun 10, 2026
…y()`

Reported by GitHub Code Quality

Follow-up to ed439a2 #698

Closes #2027
vszakats added a commit that referenced this pull request Jun 15, 2026
Functionally the same. To avoid LLMs generating false positives.

Reported-by GitHub Code Quality

Follow-up to ed439a2 #698

Closes #2068
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 5, 2026
vszakats added a commit that referenced this pull request Jul 5, 2026
Also free the buffers if set before allocating.

Reported by GitHub Code Quality and Copilot

Follow-up to ed439a2 #698

Closes #2220
vszakats added a commit to vszakats/libssh2 that referenced this pull request Jul 8, 2026
vszakats added a commit that referenced this pull request Jul 9, 2026
And the openssl local functions they called. These functions were not
called by any public interface, or tests.

Follow-up to ed439a2 #698

Closes #2260
vszakats added a commit that referenced this pull request Jul 9, 2026
vszakats added a commit that referenced this pull request Jul 12, 2026
Prior to this patch it was loading a PEM-formatted private key via
OpenSSL. When successful, it did nothing, when failed, it fell back
trying to load an OpenSSH-formatted key. This was no-op in itself, but
SK keys don't support the PEM format AFAIU, so the function went to the
OpenSSH codepath when used as intended. In both cases, it leaked the
OpenSSL memory object.

Drop the superfluous intermediate step and map this function directly to
the internal worker `ossl_key_sk_from_openssh_blob()`, also fixing the
memory leak and unnecessary processing.

Also:
- sync `passphrase` type with rest of internal APIs.

Memleak reported repeatedly by GitHub Code Quality
Follow-up to ed439a2 #698

Closes #2281
vszakats added a commit that referenced this pull request Jul 14, 2026
vszakats added a commit that referenced this pull request Jul 14, 2026
"If `SSH2_ALLOC` returns NULL, `ssh2_explicit_zero` and `memcpy` will
dereference a NULL pointer. The return value of `SSH2_ALLOC` should be
checked before use. The same issue exists in
`ossl_ecdsa_sk_openssh_priv_to_pubkey` at lines 3132-3134."

Also: drop a redundant NULL check.

Reported by GitHub Code Quality and Copilot

Follow-up to ed439a2 #698

Closes #2292
vszakats added a commit that referenced this pull request Jul 14, 2026
vszakats added a commit that referenced this pull request Jul 14, 2026
…key_sk()`

This code section was merged as part of PR #752, which seems to me
unrelated to the intent of the patch, and also possibly unrelated to
the description of its subcommit:
"Add support for certificate signing with ssh-agent"
55abcb0#diff-8213ab9c800f30c1a7aff37863954e756f946a3b3f6bd7be376d634fd9beb9f4R2369

"`session->userauth_pblc_method` is allocated in the ecdsa/ed25519
branches (lines 2393-2416), but then `userauth_read_blob_pubkey` is
called immediately afterward with `&session->userauth_pblc_method` as an
output parameter. Inside `userauth_read_blob_pubkey`, `*method` is
unconditionally overwritten with a newly allocated buffer (see the
`*method = pubkey` assignment), causing the allocation made just above
to be leaked. The allocation in the ecdsa/ed25519 branches should be
removed since `userauth_read_blob_pubkey` will derive and allocate the
method from the public key data itself."

Reported by GitHub Code Quality

Follow-up to bc4e619 #752
Follow-up to ed439a2 #698

Closes #2294
vszakats added a commit that referenced this pull request Jul 18, 2026
…y fixes

Make sure to store buffer lengths to the session structure only after
the buffer allocation was successful. To avoid leaving non-zero length
there on error cases.

Also:
- fix to fail username/password bounds check with
  `LIBSSH2_ERROR_OUT_OF_BOUNDARY` instead of conflating it with
  `LIBSSH2_ERROR_ALLOC` in `userauth_keyboard_interactive()`, and
  `userauth_password()`.

- fix to verify method sizes before calling `strncmp()` in
  `libssh2_userauth_publickey_sk()`
  Follow-up to ed439a2 #698

- replace `strlen()` calls with `sizeof()` for efficiency in
  `libssh2_userauth_publickey_sk()`
  Follow-up to ed439a2 #698

- fix error message text in `userauth_keyboard_interactive()`.
  Reported by Copilot.
  Bug: #2224 (comment)

- bail out on zero-length public key in `ssh2_userauth_publickey()`,
  to avoid zero-length allocation and imporove reported error.
  Reported by Copilot.
  Bug: #2224 (comment)

Bug: #2220 (comment)

Closes #2224
vszakats added a commit that referenced this pull request Jul 19, 2026
To actually allow loading the longer key types added to this function
after the check has been put in place.

Follow-up to ed439a2 #698
Follow-up to 0309229 #248

Closes #2352
vszakats pushed a commit that referenced this pull request Aug 19, 2026
`libssh2_userauth_publickey_sk()` gets the public key from the sk
private key via `ssh2_sk_pubkey()`, and on success that helper returns
`sk_info.application` and `sk_info.key_handle` as fresh allocations. The
cleanup frees `application` but never `key_handle`, so every successful
security-key authentication leaks it. When a caller passes both a
private blob and its own `publickeydata`, the `pubkeydata` buffer that
`userauth_read_pubkey()` allocates is also lost, since only
`tmp_publickeydata` is released.

Free `key_handle` next to `application`, and free `pubkeydata` only when
it is not the same pointer as `tmp_publickeydata` so the aliased branch
still frees once. Ownership stays with the function that received the
buffers, matching how `libssh2_userauth_publickey_fromfile()` and
`_frommemory()` already free their `pubkeydata` after
`ssh2_userauth_publickey()` returns.

Follow-up to ed439a2 #698

Closes #2530
vszakats added a commit that referenced this pull request Oct 7, 2026
…2_ALLOC`)

`SSH2_ALLOC()` (or formerly `LIBSSH2_ALLOC()`) is an internal macro, not
available for API users. Mention `LIBSSH2_ALLOC_FUNC` instead to refer
to the custom allocator set via `libssh2_session_init_ex()`. If no
custom allocator was set, it means the default system allocator.

Follow-up to ed439a2 #698

Closes #2660
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants