Skip to content

ECDSA support for mbed TLS#385

Merged
willco007 merged 10 commits intolibssh2:masterfrom
katzer:feature/mbedtls-ecdsa-support
Sep 1, 2020
Merged

ECDSA support for mbed TLS#385
willco007 merged 10 commits intolibssh2:masterfrom
katzer:feature/mbedtls-ecdsa-support

Conversation

@katzer
Copy link
Copy Markdown
Contributor

@katzer katzer commented Jul 2, 2019

About

This PR adds support for ECDSA for both key exchange and host key algorithms.

The following elliptic curves are supported:

  • 256-bit curve defined by FIPS 186-4 and SEC1
  • 384-bit curve defined by FIPS 186-4 and SEC1
  • 521-bit curve defined by FIPS 186-4 and SEC1

@katzer katzer changed the title ECDSA support for mbed TLS ECDSA support for mbed TLS - HELP NEEDED !!! Jul 2, 2019
@katzer katzer changed the title ECDSA support for mbed TLS - HELP NEEDED !!! ECDSA support for mbedTLS - HELP NEEDED !!! Jul 2, 2019
@katzer katzer changed the title ECDSA support for mbedTLS - HELP NEEDED !!! ECDSA support for mbed TLS Jul 2, 2019
@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch 7 times, most recently from 18a711f to 37efb24 Compare July 3, 2019 11:28
@katzer katzer marked this pull request as ready for review July 3, 2019 11:28
@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch 2 times, most recently from 4bc1ff2 to aa6e63e Compare July 4, 2019 09:51
@katzer
Copy link
Copy Markdown
Contributor Author

katzer commented Jul 16, 2019

@willco007 Any comments?

@tiennou
Copy link
Copy Markdown
Contributor

tiennou commented Jul 17, 2019

I'm poking at this using the WIP testcase PR (I have an ECDSA userauth testcase coming up).

Right now it's causing memory corruption, as it seems you're missing a call to mbedtls_ecdsa_init before calling mbedtls_ecdsa_genkey. With that fixed, mbedTLS complains about "PK - invalid key tag or value" when trying to parse the key, as it doesn't seem to know about OpenSSH-type keys — as per pk_parse.c.

AFAIU all our key support matrix issues are caused by our hostkey support going like this :

  • asks crypto backend to parse PEM/read DER (via init/initPEM).
  • crypto backend might use its own parser, or it might use our PEM parser (mbedTLS does the former, OpenSSL does the latter).

Hence, you might be able to crack open the key using our PEM parser, and use the things inside to build the key manually. This SO question seems to imply it's doable.

@katzer
Copy link
Copy Markdown
Contributor Author

katzer commented Jul 20, 2019

@tiennou

I'm poking at this using the WIP testcase PR (I have an ECDSA userauth testcase coming up).

👍

Right now it's causing memory corruption, as it seems you're missing a call to mbedtls_ecdsa_init before calling mbedtls_ecdsa_genkey.

Strange, for me it works. But in general you're right. It's missing there. I'll add it.

mbedTLS complains about "PK - invalid key tag or value" when trying to parse the key, as it doesn't seem to know about OpenSSH-type keys

Only PEM/DER format is supported. I successfully tested userauth and remote command execution with a ecdsa key created as follows:

ssh-keygen -m PEM -t ecdsa -b 256 -q -f $HOME/.ssh/dev.key -N ""

That creates a EC PRIVATE KEY. I dont get your point why to use another PEM parser?

@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch from aa6e63e to 668e783 Compare July 20, 2019 16:17
@tiennou
Copy link
Copy Markdown
Contributor

tiennou commented Jul 20, 2019

I did ssh-keygen -t ecdsa -f key_ecdsa, and got this key out (OpenSSH_7.9p1, LibreSSL 2.7.3, on macOS 10.14.4).

That creates a EC PRIVATE KEY. I dont get your point why to use another PEM parser?

Because the one provided by mbedTLS doesn't handle OpenSSH keys (like the one I generated above), so you're leaving that one the table compatibility-wise. Would you be so kind to PR my repo with a couple of those EC PRIVATE KEY so I can add them to the testsuite?

Hence, we have "a bunch" of pem parsers, one which knows RSA/DSA, one which knows OpenSSH. This one is used by Libgcrypt and WinCNG. OpenSSL uses our OpenSSH parser, but its own for RSA/DSA. mbedTLS uses its own parser exclusively.

Do note that I'm trying to grasp how the codebase is architected at the moment, so take what I'm saying with a grain a salt, but I feel that the mbedTLS backend should follow the same logic and go through our parser as well. And then add EC PRIVATE KEY support to our parser, so that all backends can benefit. I have a branch trying to make the parser architecture "saner", but it's not ready yet, so I can't provide a baseline…

Arguably, I was misled by the format issue, so if I get one of those keys, I'll give it a try and review. It's one less missing feature after all 😉.

@katzer
Copy link
Copy Markdown
Contributor Author

katzer commented Jul 30, 2019

@tiennou @willco007 I've also added support to handle OpenSSL keys. Would be nice if one of you could review my PR.

@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch 2 times, most recently from 97e12d9 to cda14b2 Compare July 30, 2019 15:10
@willco007
Copy link
Copy Markdown
Member

@katzer the _libssh2_mbedtls_parse_openssh_key function only handles ecdsa style keys, I assume that is just because this is a WIP. Otherwise the parsing/building of the key type looks fine to me. 👍

Copy link
Copy Markdown
Contributor

@tiennou tiennou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor review comments, please take the macro stuff with a grain of salt.

AFAICT the mbedTLS backend never supported OpenSSH keys (OPENSSH PRIVATE KEY), the current architecture makes it hard to have consistent support for stuff, so I'm fine with it only supporting ECDSA keys for now.

@katzer
Copy link
Copy Markdown
Contributor Author

katzer commented Jul 31, 2019

@tiennou

A few minor review comments, please take the macro stuff with a grain of salt.

Done

AFAICT the mbedTLS backend never supported OpenSSH keys (OPENSSH PRIVATE KEY) [...] so I'm fine with it only supporting ECDSA keys for now.

Now I've implemented it, so lets keep it.

@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch 2 times, most recently from 6225a44 to 75320e9 Compare July 31, 2019 09:47
@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch from 8085158 to 2b405c3 Compare August 21, 2019 08:40
@katzer
Copy link
Copy Markdown
Contributor Author

katzer commented Aug 21, 2019

@willco007 @tiennou Any chance to see that being merged into master any time soon?

@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch from 2b405c3 to 6bc4b0b Compare September 26, 2019 14:00
@stale
Copy link
Copy Markdown

stale bot commented Dec 25, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch from 0490bfa to 882b8b2 Compare March 17, 2020 17:41
@willco007
Copy link
Copy Markdown
Member

What state are we in with this, it looks ready to be merged, is that correct?

@katzer
Copy link
Copy Markdown
Contributor Author

katzer commented Apr 15, 2020

@willco007 Yes

Copy link
Copy Markdown
Member

@willco007 willco007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a couple comments about additional bounds checking, but otherwise it looks great. Thanks for the PR!

src/mbedtls.c Outdated
p += 4;
*p = 0;

mbedtls_mpi_write_binary(mpi, p + 1, bytes - 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also advancing p here, and if bytes is 0 length this could go negative. Same below in the memmove.

@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch 2 times, most recently from b82ae61 to 306a519 Compare June 2, 2020 05:49
@willco007
Copy link
Copy Markdown
Member

This PR is still waiting on bounds checks updates I noted on May 15, thanks.

@katzer katzer force-pushed the feature/mbedtls-ecdsa-support branch from 306a519 to d9f4509 Compare July 11, 2020 09:39
@katzer katzer requested a review from willco007 July 12, 2020 09:57
@willco007 willco007 merged commit 5528f3d into libssh2:master Sep 1, 2020
willco007 pushed a commit to willco007/libssh2 that referenced this pull request Sep 2, 2020
Files:
mbedtls.c, mbedtls.h, .travis.yml

Notes:
This PR adds support for ECDSA for both key exchange and host key algorithms.

The following elliptic curves are supported:

256-bit curve defined by FIPS 186-4 and SEC1
384-bit curve defined by FIPS 186-4 and SEC1
521-bit curve defined by FIPS 186-4 and SEC1

Credit:
Sebastián Katzer
cy384 pushed a commit to cy384/opentransport-libssh2 that referenced this pull request Jan 9, 2021
Files:
mbedtls.c, mbedtls.h, .travis.yml

Notes:
This PR adds support for ECDSA for both key exchange and host key algorithms.

The following elliptic curves are supported:

256-bit curve defined by FIPS 186-4 and SEC1
384-bit curve defined by FIPS 186-4 and SEC1
521-bit curve defined by FIPS 186-4 and SEC1

Credit:
Sebastián Katzer
vszakats referenced this pull request Apr 25, 2024
This is just a stub to make `_libssh2_mbedtls_ecdsa_new_private`
compile.

mbedtls 3.6.0 silently deleted its public API `mbedtls_pk_load_file`,
which this function relies on.

Closes #1349
@katzer katzer deleted the feature/mbedtls-ecdsa-support branch January 5, 2026 14:33
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.

5 participants