Skip to content

mbedtls: expose mbedtls_pk_load_file() for our use - #1421

Closed
orangepizza wants to merge 3 commits into
libssh2:masterfrom
orangepizza:mbedtls36
Closed

orangepizza wants to merge 3 commits into
libssh2:masterfrom
orangepizza:mbedtls36

Conversation

@orangepizza

Copy link
Copy Markdown
Contributor

while it's moved to pk_internal, it won't removed in mbedtls 3.6 LTS
so it's safe to redeclare it on our side to find it

this is implementing emergency fix suggested from
2e4c5ec#commitcomment-141379351

@orangepizza

Copy link
Copy Markdown
Contributor Author

I limited this patch only enabled in mbedtls 3.6.x , so any version newer than that 4.x or 3.7 etc, someone need to test/look at upstream code if patch is still appropriate

@vszakats

Copy link
Copy Markdown
Member

My concern is that the lazy fix just postpones this tech debt invisibly into the future.

Is there a way to reimplement this call using supported APIs?

@orangepizza

Copy link
Copy Markdown
Contributor Author

My concern is that the lazy fix just postpones this tech debt invisibly into the future.

Is there a way to reimplement this call using supported APIs?

to do that it looks like you need to manually set ecdsa curve parameter from ecp.h (don't think it'd safe) or grab or write raw byte fileread function form somewhere else and tose it into _libssh2_mbedtls_ecdsa_new_private_frommemory (this drops support for pem though)
from looks of it mbedtls_pk_load_file is just read file x and put it in data.

@vszakats

Copy link
Copy Markdown
Member

Using low-level crypto (or alternate internals?) would of course not be an ideal solution.

@gilles-peskine-arm suggested mbedtls_pk_parse_key(). Could this be useful?

@orangepizza

orangepizza commented Jul 13, 2024 •

Copy link
Copy Markdown
Contributor Author

that returns mbedtls_pk_context, ecp.h needed to convert it into mbedtls_ecdsa_context (which is rename of mbedtls_ecp_keypair)
so if we manually touch mbedtls_ecdsa_context we need to handle ecp.
edit: never mind , I found mbedtls_pk_ec

https://github.com/Mbed-TLS/mbedtls/blob/f974bff38c958f1f3ca997253f002a7b179952ab/tf-psa-crypto/drivers/builtin/include/mbedtls/ecdsa.h#L67
https://github.com/Mbed-TLS/mbedtls/blob/development/tf-psa-crypto/drivers/builtin/include/mbedtls/ecp.h#L415

@orangepizza

Copy link
Copy Markdown
Contributor Author

tried mimicing what rsa function did, but ecdsa_context isn't something able to copied by public (no mbedtls_ecdsa_keypair_copy)
I can just point *ctx into ec keypair inside pk_context structure, but doing so will cause leak on other members of pk_context structure (because no we can't clean pkey without nulling ecdsa

Comment thread src/mbedtls.h Resolved
Comment thread src/mbedtls.c Resolved
@vszakats

vszakats commented Jul 13, 2024 •

Copy link
Copy Markdown
Member

I'm still bummed why mbedTLS is removing a function with no replacement. This combined with no notice or a line in the ChangeLog is causing unnecessary pain.

@vszakats vszakats changed the title expose mbedtls_pk_load_file for our use mbedtls: expose mbedtls_pk_load_file() for our use Jul 13, 2024
@vszakats vszakats added the build label Jul 13, 2024
@vszakats

Copy link
Copy Markdown
Member

Thanks. Would you mind rebasing to master? And delete the _LIBSSH2_DISABLE_MBEDTLS36_PK_LOAD_FILE references I mentioned? those become unused after this patch.

while it's moved to pk_internal, it won't removed in mbedtls 3.6 LTS
so it's safe to redeclare it on our side to find it
@orangepizza

Copy link
Copy Markdown
Contributor Author

rebased: should I squash those too?

@vszakats

Copy link
Copy Markdown
Member

No need, thanks.

@vszakats vszakats closed this in 1628f6c Jul 13, 2024
@vszakats

Copy link
Copy Markdown
Member

Merged now, thanks you for the PR!

agreppin pushed a commit to agreppin/libssh2 that referenced this pull request Jul 14, 2024
While it's moved to pk_internal, it won't removed in mbedTLS 3.6 LTS
so it's safe to redeclare it on our side to find it.

This is implementing emergency fix suggested from
libssh2@2e4c5ec#commitcomment-141379351

Follow-up to e973493 libssh2#1393
Follow-up to 2e4c5ec libssh2#1349
Closes libssh2#1421
willco007 added a commit that referenced this pull request Jul 22, 2024
Fix regression with commit from #1421
willco007 added a commit that referenced this pull request Jul 22, 2024
Fix regression with commit from #1421
vszakats added a commit that referenced this pull request Apr 24, 2026
This mbedTLS API is no longer exported by mbedTLS 3.6.0+. Replace with
local implementation.

Limit size of loaded keys to 1 MiB.

Follow-up to 1628f6c #1421
Follow-up to e973493 #1393
Follow-up to 2e4c5ec #1349

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants