Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove OpenSSL #955

Merged
merged 4 commits into from
Dec 5, 2023
Merged

Remove OpenSSL #955

merged 4 commits into from
Dec 5, 2023

Conversation

bettio
Copy link
Collaborator

@bettio bettio commented Nov 20, 2023

Do not depend both on Mbed-TLS and OpenSSL at the same time, just use Mbed-TLS.
Also now the same code base can be both used on generic unix and MCUs.

Closes #950

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

src/libAtomVM/otp_crypto.c Outdated Show resolved Hide resolved
@bettio bettio marked this pull request as draft November 20, 2023 22:51
@bettio bettio force-pushed the remove-openssl branch 4 times, most recently from c69faf6 to 7510cf5 Compare November 26, 2023 12:33
@bettio bettio marked this pull request as ready for review November 26, 2023 16:37

#ifdef ATOMVM_HAS_MBEDTLS
#ifndef AVM_NO_SMP
Mutex *entropy_mutex;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I understand these mutexes are only used for initialization. We don't even need to acquire them if the boolean is true.

More simply, why don't we initialize the mbed tls contexts in sys_init_platform?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I gather the info also here: mutex are used for locking when mbedtls is built without threading support.

We also use them for avoding race condition when doing async init.

#ifndef AVM_NO_SMP
Mutex *entropy_mutex;
#endif
mbedtls_entropy_context entropy_ctx;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We probably want the mbed tls contexts to be in GlobalContext and be available in all platforms unless ATOMVM_HAS_MBEDTLS is undefined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd like to do some further decoupling while iterating over, so I don't like turning GlobalContext into a kitchen sink.

So the plan is, as mentioned in #930, shipping some features using common components and libraries, and then features such as mbedtls are picked up from platforms supporting it (+ implementing the missing pieces).

src/platforms/generic_unix/lib/sys.c Show resolved Hide resolved
@bettio bettio force-pushed the remove-openssl branch 2 times, most recently from db622ed to 57a12a6 Compare November 28, 2023 12:23
Minimal implementation of crypto:strong_rand_bytes/1 using mbedtls.

Signed-off-by: Davide Bettio <davide@uninstall.it>
Add test for atomvm:random/0 so implementation can be replaced with a
different implementation.

Signed-off-by: Davide Bettio <davide@uninstall.it>
Use Mbed-TLS based crypto:strong_rand_bytes/1 for generating random
numbers rather than OpenSSL.

Signed-off-by: Davide Bettio <davide@uninstall.it>
@bettio bettio force-pushed the remove-openssl branch 4 times, most recently from 9581fea to 10a3195 Compare November 29, 2023 00:08
mbedtls_ctr_drbg_init(&platform->random_ctx);

mbedtls_entropy_context *entropy_ctx = sys_mbedtls_get_entropy_context_lock(global);
// Safe to unlock it now, sys_mbedtls_entropy_func will lock it again later
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not understand this and the locking pattern here.

  1. Isn't locking only meant when MBEDTLS_THREADING_C is not defined?
  2. Are we just fetching entropy context here without locking it? If so, could we simplify the logic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We do locking as part of async initialization and because we don't know what the caller will do afterwards.
Since here we just interact with entropy through the entropy function we can just release the lock as the first thing.

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved

int sys_mbedtls_entropy_func(void *entropy, unsigned char *buf, size_t size)
{
#ifndef MBEDTLS_THREADING_C
Copy link
Collaborator

@pguyot pguyot Nov 29, 2023

Choose a reason for hiding this comment

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

In fact, if we are building for multicore, we should define MBEDTLS_THREADING_C on esp32 & pico for a finer-grained locking directly managed by mbedtls (they know when to lock and unlock).
On esp32, it's disabled by default but it's part of the sdk config.
On Pico, we provide our own mbedtls-config.h, so we can just add it there.

Then we need to define MBEDTLS_THREADING_ALT and call mbedtls_threading_set_alt.
On esp32, this is defined by default.
On Pico, we need to add it to mbedtls-config.h as well.

On generic unix, we don't control how mbedtls was compiled, so it makes sense to keep this logic and handle the locking ourselves if MBEDTLS_THREADING_C is undefined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did a quick check and they don't look much more fine grained than this implementation.
On ESP32 I didn't want to alter the default configuration and I wanted to support both scenarios, since supporting them didn't cost a large effort I just implemented that wrapper function and handled both configurations.
Right now we customize sdkconfig just for partitioning and that's it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right.

Still we probably want to use mbedtls native threading on Pico as we're providing our own config file.

Copy link
Collaborator Author

@bettio bettio Dec 3, 2023

Choose a reason for hiding this comment

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

I would add a TODO, since this requires extra work for providing rp2040 specific synchronization helpers to mbedtls.

Copy link
Collaborator

@pguyot pguyot left a comment

Choose a reason for hiding this comment

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

Do we want ssl to use this shared entropy as part of this PR?

I would keep the scope of this PR limited to this.

if (!platform->random_is_initialized) {
mbedtls_ctr_drbg_init(&platform->random_ctx);

mbedtls_entropy_context *entropy_ctx = sys_mbedtls_get_entropy_context_lock(global);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am still not a fan of this.

If would deadlock if first calling code first gets entropy and then ctr_drbg.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, but even removing this doesn't completely fix the problem: it might be still hidden in implicit use of entropy from ctr_drbg.
My only valid suggestion here is telling very clearly that ctr_drbg shouldn't be used at all when owning entropy lock.


int sys_mbedtls_entropy_func(void *entropy, unsigned char *buf, size_t size)
{
#ifndef MBEDTLS_THREADING_C
Copy link
Collaborator

Choose a reason for hiding this comment

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

Right.

Still we probably want to use mbedtls native threading on Pico as we're providing our own config file.

Copy link
Collaborator

@fadushin fadushin left a comment

Choose a reason for hiding this comment

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

I vote we merge and leave TODOs in the relevant parts of the code.

Define also sys_mbedtls.h header and sys_mbedtls_get_entropy_context_lock and
sys_mbedtls_get_ctr_drbg_context_lock functions.

Signed-off-by: Davide Bettio <davide@uninstall.it>
@bettio bettio merged commit dfcfc96 into atomvm:master Dec 5, 2023
84 checks passed
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.

Port RNG code to mbedtls (and deprecate OpenSSL)
3 participants