Skip to content

mbedTLS crypto backend - #106

Closed
wildart wants to merge 11 commits into
libssh2:masterfrom
wildart:mbedtls
Closed

wildart wants to merge 11 commits into
libssh2:masterfrom
wildart:mbedtls

Conversation

@wildart

@wildart wildart commented May 8, 2016

Copy link
Copy Markdown
Contributor

This is mbedTLS crypto backend (libmbedcrypto) for libssh2 cryptography.

@EricForgy

Copy link
Copy Markdown

Hi libssh2 :)

For what it's worth, this would be super helpful for Julia users with private GitHub repos as discussed in the issue @wildart referenced above. Cheers

@bagder

bagder commented May 8, 2016

Copy link
Copy Markdown
Member

@EricForgy, is that an "independent" claim that the patch works as intended?

@EricForgy

Copy link
Copy Markdown

Hi @bagder, not an independent claim (I'm not qualified). Just morale support :)

If it helps, I'll see if someone in that Julia issue can help with an independent verification.

@wildart, is there some simple thing I can do to help? It was a challenge for me just to get SSH working with GitHub in the first place, so I'm just hoping Julia gets fixed.

@wildart
wildart force-pushed the mbedtls branch 3 times, most recently from 09f7c8d to 8754165 Compare May 8, 2016 22:02
@wildart

wildart commented May 8, 2016 •

Copy link
Copy Markdown
Contributor Author

I changed travis configuration to include to mbedtls support.

Comment thread cmake/FindmbedTLS.cmake Outdated
NAMES mbedtls libmbedtls libmbedx509
PATHS
${MBEDTLS_ROOT_DIR}/library
${MBEDTLS_ROOT_DIR}/build/library

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

these should probably refer to an install tree, not a build tree (which certainly isn't guaranteed to be called "build")

@Keno

Keno commented May 19, 2016

Copy link
Copy Markdown
Contributor

What's the status of this? What's needed to move this forward?

@Keno

Keno commented May 31, 2016

Copy link
Copy Markdown
Contributor

Just another friendly ping. I'd be happy to help out with whatever is needed here.

@bagder

bagder commented May 31, 2016

Copy link
Copy Markdown
Member

@Keno did you try it out?

Comment thread mbedtls/buildmbedtls
# download mbedtls sources
cd mbedtls
wget https://github.com/ARMmbed/mbedtls/archive/$MBEDTLS_SRC_ARCH.tar.gz
tar -xzf $MBEDTLS_SRC_ARCH.tar.gz

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I realize that this file mainly exists to set up testing on TravisCI, but this doesn't work on macOS because wget is not available by default. You could conditionally substitute wget with curl -O -L. Alternatively, you could replace this file with something in the top-level CMakeLists.txt with the ExternalProject_Add command. I'm winging this, but it would be something like:

SET(MBEDTLS_SRC_ARCH mbedtls-2.2.1)
ExternalProject_Add(
  URL https://github.com/ARMmbed/mbedtls/archive/${MBEDTLS_SRC_ARC}.tar.gz
  PATCH patch -p0 < ../mbedtls.patch
  CMAKE_ARGS -DCMAKE_BUILD_TYPE=RelWithDebInfo -DUSE_SHARED_MBEDTLS_LIBRARY=ON
  BUILD_COMMAND make
  INSTALL_COMMAND make install
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ExternalProject is executed during the build time, however mbedtls installation is needed before the configuration step.

@Keno

Keno commented Jul 6, 2016

Copy link
Copy Markdown
Contributor

I finally got around to trying this out and it worked for me on all platforms that I tried (Linux, Mac, Windows).

@tkelman

tkelman commented Jul 18, 2016

Copy link
Copy Markdown
Contributor

I was also able to get this to pass the tests on x86_64 ubuntu 14.04 via

git clone https://github.com/ARMmbed/mbedtls -b mbedtls-2.2.1
git clone https://github.com/wildart/libssh2 -b mbedtls
mkdir -p mbedtls/build
mkdir -p libssh2/build
cd mbedtls
patch -p0 < ../libssh2/mbedtls/mbedtls.patch
cd build
cmake .. -DUSE_SHARED_MBEDTLS_LIBRARY=ON -DCMAKE_INSTALL_PREFIX=$PWD/../../usr
make -j`nproc` install
cd ../../libssh2/build
cmake .. -DCRYPTO_BACKEND=mbedTLS -DCMAKE_INSTALL_PREFIX=$PWD/../../usr
make -j`nproc` install
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD/../../usr/lib ctest .

Comment thread src/mbedtls.c Outdated
if(!ret)
ret = mbedtls_cipher_finish(ctx, output + olen, &finish_olen);

olen += finish_olen;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If mbedtls_ciper_reset() fails, olen and finish_olen will still be uninitialized. Adding them and using them in the _libssh2_mbedtls_safe_free() below will have unwanted behaviour.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

if any of the mbedtls_ calls fail then memcpy will not be executed with olen as parameter.
BTW, _libssh2_mbedtls_safe_free is called with osize not olen.

@pjbakker

Copy link
Copy Markdown

Did a quick pass over the PR.. As I'm not that familiar with the libssh2 internals, I cannot comment too much on correctness. I flagged things that seemed worth a second look.

Comment thread src/mbedtls.c Outdated
return -1; /* failure */

ret = mbedtls_rsa_pkcs1_verify(rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
MBEDTLS_MD_SHA1, sig_len, hash, sig);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The length parameter here is the length of the hash, not the length of the signature, which is assumed to be rsa->len. That assumption should be verified, and SHA_DIGEST_LENGTH should be passed here.

Comment thread src/mbedtls.c
return -1;
}

mbedtls_rsa_context *pk_rsa = mbedtls_pk_rsa(pkey);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verify that this is actually an RSA context first?

Comment thread src/mbedtls.c Outdated
/* Zero unsued bits above the most significant bit and if `top` is -1,
the most significant bit of the random number can be zero. */
msb = bits;
if (top == -1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct. "Can be zero" just means that it's not forced to 1 as the other values of top do.

Comment thread src/mbedtls.c Outdated
to 1, so that the product of two such random numbers will always have 2*bits length.
*/
for(i=0;i<=top;++i) {
err = mbedtls_mpi_set_bit(bn, bits-i, 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bits - i - 1? It seems to me that the most significant bit is bits - 1, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought bits is index of the most significant bit. because from kex.c _libssh2_bn_rand is called with following parameters

_libssh2_bn_rand(exchange_state->x, group_order * 8 - 1, 0, -1);

Why subtract 1, other then adjusting to 0-based bit index.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Well, bits is definitely the number of bits of randomness, and we know bits are zero indexed, so say we want one bit of randomness, then 0 is the MSB which is bits-1, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure.

@tkelman

tkelman commented Sep 16, 2016

Copy link
Copy Markdown
Contributor

@wildart is there remaining review to address here as far as you're concerned?

@Keno what are your thoughts? If needed, would you be able to take this over so we can finish upstreaming it?

@Keno

Keno commented Sep 16, 2016

Copy link
Copy Markdown
Contributor

I believe @wildart has addressed all the review comments I had. This is good to go from my perspective.

@tkelman

tkelman commented Sep 24, 2016

Copy link
Copy Markdown
Contributor

Can any of the libssh2 maintainers review this please? Julia has been carrying this patch for a month or two and it's been working for our purposes. We want to get this PR upstreamed some day. What do you need us to do to get this moving in that direction, even slowly?

@bagder

bagder commented Sep 25, 2016 •

Copy link
Copy Markdown
Member

Can you then please squash the patch series properly? Reviewing a patch series with some of the patches fixing mistakes in some of the earlier patches isn't very effective. Squash those that fix mistakes so that you only have distinct commits that each makes a logical change.

@tkelman

tkelman commented Sep 25, 2016

Copy link
Copy Markdown
Contributor

@Keno do you have collaborator access to @wildart's fork to do that?

Can also look at https://github.com/libssh2/libssh2/pull/106/files which just shows the end result.

@bagder

bagder commented Sep 25, 2016

Copy link
Copy Markdown
Member

If the end result is the only thing to review, then the PR should be made as a single commit.

Any considerations on fixing the autotools build script to support mbedTLS? Shouldn't we have some docs somewhere that describes this support and perhaps how you build libssh2 to use it?

@tkelman

tkelman commented Sep 26, 2016

Copy link
Copy Markdown
Contributor

Yes, docs are an obvious omission. @wildart has been mia for a while so we'll have to either find someone with commit access to the fork (@Keno? hopefully he'll respond, maybe during the week) or open a new revised copy of the pr.

mbedtls does not have an autotools build system itself. It has a simple makefile but I don't know if it's enough for our purposes.

@Keno

Keno commented Sep 26, 2016

Copy link
Copy Markdown
Contributor

Yes, I should have access to wildart's fork. I'll squash this.

@Keno

Keno commented Sep 26, 2016

Copy link
Copy Markdown
Contributor

Turns out I don't. Opening a new PR.

@bagder

bagder commented Sep 26, 2016

Copy link
Copy Markdown
Member

continued in #132

@bagder bagder closed this Sep 26, 2016
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.

7 participants