How to print the SSH host key as a client

Jente Hidskes jente.hidskes at klarna.com
Wed Feb 9 11:51:46 CET 2022


D'oh, that was it. Thanks Ján!

On Wed, Feb 9, 2022 at 11:45 AM Ján Osuský <jan.osusky at iblsoft.com> wrote:
>
> Oh, I see, the key is not a string, it contains binary data and (usually) starts with a zero byte :-).
>
> On Wednesday 9. February 2022, 11:30:15 (+01:00), Jente Hidskes wrote:
>
> > Hi Ján,
> >
> > Thanks for your reply! I quickly tested with libssh2_session_startup
> > but it makes no difference unfortunately. It's good to know what I
> > need is possible with libssh2, though!
> >
> > I did find that if I retrieve the length of the key, I get 279 (and
> > the key type is PLAIN). Yet printing it still returns the empty
> > string.
> > To show what I mean, here's the output of my code:
> >
> > $ ./sftp 10.241.167.53 22 user pass
> > Fingerprint: 28 75 36 2E B2 8B E8 8D 87 63 28 9F 42 0E D1 6F 2D 8B 38 EE
> > Host key: ""
> > Host key type: 1
> > Host key length: 279
> > all done
> >
> > I uploaded my example code here[1], in case it helps. I am using libssh2 1.10.0.
> >
> > Thanks,
> >
> > Jente
> >
> > [1] https://gist.github.com/Hjdskes/898cd7389b447f75c9ca0798097ac436
> >
> > Jente Hidskes
> >
> > Engineer
> >
> > Nordic Payment Networks
> >
> > +46728558908
> >
> > Klarna Bank AB (publ)
> >
> > Sveavägen 46, 111 34 Stockholm
> >
> > Tel: +46 8 120 120 00
> >
> > Reg no: 556737-0431
> >
> > klarna.com
> >
> >
> >
> >
> > On Wed, Feb 9, 2022 at 11:12 AM Ján Osuský <jan.osusky at iblsoft.com> wrote:
> > >
> > > Hi Jente,
> > >
> > > we use libssh2 for a client implementation too and we use both the fingerprint (with libssh2_hostkey_hash) and the host key itself.
> > >
> > > Function "libssh2_session_hostkey" returns pointer to the key as expected, and also sets the key length and type.
> > >
> > > In our code (that is not new anymore) we still use libssh2_session_startup instead of libssh2_session_handshake but I don't think that should be a problem.
> > >
> > >
> > > Best regards,
> > >
> > >
> > > JanO
> > >
> > >
> > >
> > > On Wednesday 9. February 2022, 10:17:05 (+01:00), Jente Hidskes via libssh2-devel wrote:
> > >
> > > > Hey folks,
> > > >
> > > > I am working on a small SFTP client using libssh2. I would like for
> > > > this client to persist the host key of the server it is connecting to,
> > > > but I seem to be unable to get a hold of it.
> > > >
> > > > After initialising libssh2 and calling libssh2_session_handshake, I am
> > > > able to print the host key fingerprint through libssh2_hostkey_hash as
> > > > in the example programs. I assumed I would be able to print the actual
> > > > host key rather than the fingerprint with libssh2_session_hostkey, but
> > > > that returns an empty string.
> > > >
> > > > I suspect that libssh2_session_hostkey only returns the local host key
> > > > when operating as a server (given this[1] source code comment), but
> > > > the docs[2] are unclear about this: they just state "get the remote
> > > > key" and "returns a pointer to the current host key".
> > > >
> > > > Am I doing something wrong, or is it indeed impossible right now to
> > > > print the host key when operating as a client?
> > > >
> > > > Kind regards,
> > > >
> > > > Jente
> > > >
> > > > [1] https://github.com/libssh2/libssh2/blob/2a2aaed3b6c3c1dc25e35e11afcfb23f88a18510/src/libssh2_priv.h#L628
> > > > [2] https://www.libssh2.org/libssh2_session_hostkey.html
> > >
> >
> --
> Ján Osuský (Jan.Osusky at iblsoft.com)
> IBL Software Engineering, http://www.iblsoft.com/
> Galvaniho 17/C, 82104 Bratislava, Slovakia
> Phone: +421-2-32662111, Fax: +421-2-32662110


More information about the libssh2-devel mailing list