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

OPENSSL_init_ssl fails in RUN_ONCE and ossl_init_config #7350

Closed
MonkeybreadSoftware opened this issue Oct 4, 2018 · 18 comments
Closed

OPENSSL_init_ssl fails in RUN_ONCE and ossl_init_config #7350

MonkeybreadSoftware opened this issue Oct 4, 2018 · 18 comments

Comments

@MonkeybreadSoftware
Copy link

After upgrade to openssl 1.1.1 we got an issue that all the SSL stuff failed to work.

OPENSSL_init_ssl returned failure due to failure in OPENSSL_init_crypto before.
OPENSSL_init_crypto calls this block:

    if (opts & OPENSSL_INIT_LOAD_CONFIG) {
        int ret;
        CRYPTO_THREAD_write_lock(init_lock);
        appname = (settings == NULL) ? NULL : settings->appname;
        ret = RUN_ONCE(&config, ossl_init_config);
        CRYPTO_THREAD_unlock(init_lock);
        if (!ret)
            return 0;
    }

and that needs to be commented out of us to make it work again.
It looks like that with multiple invocations of OPENSSL_init_crypto in our code and used libraries, the second invocation fails and returns the error here. Not sure why, but now everything works again as it did with older OpenSSL version so far.

@MonkeybreadSoftware
Copy link
Author

we call e.g.

int r1 = OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
int r2 = OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);

for our library to init.

@mattcaswell
Copy link
Member

What platform are you running on?
Do you get any error message?
You say that the second invocation fails. Does that mean the first invocation succeeds?

@kaduk
Copy link
Contributor

kaduk commented Oct 5, 2018

(As a workaround you should be able to just remove both the init calls from your code; 1.1.1 will auto-initialize itself. But that does not keep us from wanting to track down what's going on here.)

@MonkeybreadSoftware
Copy link
Author

We use it on Mac, Windows and Linux. But I saw the problem both on Mac and Windows as we didn't test for Linux this time.

I can check in the next days for some details.

@MonkeybreadSoftware
Copy link
Author

The problem is precisely that

ret = RUN_ONCE(&config, ossl_init_config);

returns 0, which than returns a failure from OPENSSL_init_crypto.
I see ossl_init_config always returns 1, so that may not be the problem.
But maybe some problem with RUN_ONCE returning zero?

Can you guys verify that OPENSSL_init_crypto works, if you call it several times with various parameters like OPENSSL_INIT_LOAD_CONFIG or OPENSSL_INIT_NO_LOAD_CONFIG?

@ejoerns
Copy link

ejoerns commented Nov 1, 2018

We just fell across the same issue, when calling OPENSSL_init_crypto() (for using CMS functions) with OPENSSL_INIT_NO_LOAD_CONFIG followed by libcurl that calls OPENSSL_init_ssl() which itself calls OPENSSL_init_crypto() with OPENSSL_INIT_LOAD_CONFIG. This results in an error.

@michaelolbrich
Copy link

The problem is that ossl_init_config and ossl_init_no_config share the RUN_ONCE control argument. This means that if ossl_init_no_config was call once, then ossl_init_config will never be called.

The return value for RUN_ONCE is a static variable derived from the function name and initialized with '0'. In the failing case, this value is never changed, because the function is never called.

Several other option pairs have the same problem.

ejoerns added a commit to ejoerns/rauc that referenced this issue Nov 2, 2018
This will now load OpenSSL configuration from openssl.cnf default
section 'openssl_conf'.

This might be useful, anyway, but the main reason we have to change this
is to stay consistent with upcoming support for OpenSSL 1.1.1.

We also link again libcurl and libcurl uses OPENSSL_init_ssl() which
itself calls OPENSSL_init_crypto() with OPENSSL_INIT_LOAD_CONFIG.
Now, due to a bug/feature in OpenSSL, it is no more possible to initialize
OpenSSL with both 'load config' and 'no config' (refer
openssl/openssl#7350 for details and
discussion).
This forces us to initialize OpenSSL with 'load config', too when we want
to support OpenSSL 1.1.1.

This is a preparation that changes the old code to gain the same
behavior that future OpenSSL-1.1.1 supporting code will have as we do
not want to behave differently with different OpenSSL versions.

Signed-off-by: Enrico Joerns <ejo@pengutronix.de>
ejoerns added a commit to ejoerns/rauc that referenced this issue Nov 2, 2018
This will now load OpenSSL configuration from openssl.cnf default
section 'openssl_conf'.

This might be useful, anyway, but the main reason we have to change this
is to stay consistent with upcoming support for OpenSSL 1.1.1.

We also link again libcurl and libcurl uses OPENSSL_init_ssl() which
itself calls OPENSSL_init_crypto() with OPENSSL_INIT_LOAD_CONFIG.
Now, due to a bug/feature in OpenSSL, it is no longer possible to initialize
OpenSSL with both 'load config' and 'no config' (refer
openssl/openssl#7350 for details and
discussion).
This forces us to initialize OpenSSL with 'load config', too, when we want
to support OpenSSL 1.1.1.

This is a preparation that changes the old code to gain the same
behavior that future OpenSSL-1.1.1-supporting code will have as we do
not want to behave differently with different OpenSSL versions.

Signed-off-by: Enrico Joerns <ejo@pengutronix.de>
@richardweinberger
Copy link

I ran in into this issue too.
My legacy application used OPENSSL_no_config(), on 1.1.x SSL_CTX_new() started to return NULL because internally OPENSSL_init_ssl() was called and failed. :-(

@tmshort
Copy link
Contributor

tmshort commented Feb 14, 2019

I am running into this issue. Because of this in OPENSSL_init_ssl():

    if (!OPENSSL_init_crypto(opts
#ifndef OPENSSL_NO_AUTOLOAD_CONFIG
                             | OPENSSL_INIT_LOAD_CONFIG
#endif
                             | OPENSSL_INIT_ADD_ALL_CIPHERS
                             | OPENSSL_INIT_ADD_ALL_DIGESTS,
                             settings))

Any attempt to use OPENSSL_INIT_NO_LOAD_CONFIG, OPENSSL_INIT_NO_ADD_ALL_CIPHERS or OPENSSL_INIT_NO_ADD_ALL_DIGESTS before creating an SSL_CTX with SSL_CTX_new() (if that's the first thing you typically do) will cause SSL_CTX_new() to fail, prevent all use of TLS. While the NO_ADD options are unlikely to be used, I do see the OPENSSL_INIT_NO_LOAD_CONFIG being used to prevent loading configuration.

@mattcaswell
Copy link
Member

This should be fixed in git (commit f725fe5 for the 1.1.1 branch). Please try out the latest version in OpenSSL_1_1_1-stable and see if that solves the problem.

@tmshort
Copy link
Contributor

tmshort commented Feb 14, 2019

Was this fixed by PR #7983 ?

@tmshort
Copy link
Contributor

tmshort commented Feb 14, 2019

@mattcaswell just found that... but it's not in a tagged release, yet.

@mattcaswell
Copy link
Member

Was this fixed by PR #7983 ?

Yes, one of the commits in that PR fixed a RUN_ONCE bug that causes this issue.

@mattcaswell just found that... but it's not in a tagged release, yet.

Correct this is git only so far. The fix will be in 1.1.1b

@tmshort
Copy link
Contributor

tmshort commented Feb 14, 2019

Correct this is git only so far. The fix will be in 1.1.1b

Yeah, we deal with the tagged releases, so I'm not spending all my time rebasing our changes. ;) Any idea when 1.1.1b might come out? (It's been, what, almost 3 months since 1.1.1a?)

@mattcaswell
Copy link
Member

We tend to do a release every 3-4 months (unless a security issue causes us to do one earlier) ....so "soon".

@tmshort
Copy link
Contributor

tmshort commented Feb 14, 2019

Thought so... Thanks!

DeckerSU added a commit to DeckerSU/komodo that referenced this issue Apr 22, 2019
More info:

- openssl/openssl#7350
- openssl/openssl@f725fe5

Without this patch we will get following error:

```
SSL: couldn't create a context: error:00000000:lib(0):func(0):reason(0)
```

during trying to connect HTTPS.
@xnox
Copy link
Contributor

xnox commented Jun 13, 2019

Given that 1.1.1b is out, should this issue be closed?

@MonkeybreadSoftware
Copy link
Author

Well, I think for now you can close this.

leiflm pushed a commit to leiflm/rauc that referenced this issue Sep 26, 2019
This will now load OpenSSL configuration from openssl.cnf default
section 'openssl_conf'.

This might be useful, anyway, but the main reason we have to change this
is to stay consistent with upcoming support for OpenSSL 1.1.1.

We also link again libcurl and libcurl uses OPENSSL_init_ssl() which
itself calls OPENSSL_init_crypto() with OPENSSL_INIT_LOAD_CONFIG.
Now, due to a bug/feature in OpenSSL, it is no longer possible to initialize
OpenSSL with both 'load config' and 'no config' (refer
openssl/openssl#7350 for details and
discussion).
This forces us to initialize OpenSSL with 'load config', too, when we want
to support OpenSSL 1.1.1.

This is a preparation that changes the old code to gain the same
behavior that future OpenSSL-1.1.1-supporting code will have as we do
not want to behave differently with different OpenSSL versions.

Signed-off-by: Enrico Joerns <ejo@pengutronix.de>
nginx-hg-mirror pushed a commit to nginx/nginx that referenced this issue Aug 7, 2023
With this change, the NGX_OPENSSL_NO_CONFIG macro is defined when nginx
is asked to build OpenSSL itself.  And with this macro automatic loading
of OpenSSL configuration (from the build directory) is prevented unless
the OPENSSL_CONF environment variable is explicitly set.

Note that not loading configuration is broken in OpenSSL 1.1.1 and 1.1.1a
(fixed in OpenSSL 1.1.1b, see openssl/openssl#7350).
If nginx is used to compile these OpenSSL versions, configuring nginx with
NGX_OPENSSL_NO_CONFIG explicitly set to 0 might be used as a workaround.
lonerr pushed a commit to webserver-llc/angie that referenced this issue Aug 23, 2023
With this change, the NGX_OPENSSL_NO_CONFIG macro is defined when nginx
is asked to build OpenSSL itself.  And with this macro automatic loading
of OpenSSL configuration (from the build directory) is prevented unless
the OPENSSL_CONF environment variable is explicitly set.

Note that not loading configuration is broken in OpenSSL 1.1.1 and 1.1.1a
(fixed in OpenSSL 1.1.1b, see openssl/openssl#7350).
If nginx is used to compile these OpenSSL versions, configuring nginx with
NGX_OPENSSL_NO_CONFIG explicitly set to 0 might be used as a workaround.

--HG--
branch : nginx
rbg pushed a commit to grumpylabs/freenginx that referenced this issue Feb 15, 2024
With this change, the NGX_OPENSSL_NO_CONFIG macro is defined when nginx
is asked to build OpenSSL itself.  And with this macro automatic loading
of OpenSSL configuration (from the build directory) is prevented unless
the OPENSSL_CONF environment variable is explicitly set.

Note that not loading configuration is broken in OpenSSL 1.1.1 and 1.1.1a
(fixed in OpenSSL 1.1.1b, see openssl/openssl#7350).
If nginx is used to compile these OpenSSL versions, configuring nginx with
NGX_OPENSSL_NO_CONFIG explicitly set to 0 might be used as a workaround.
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

No branches or pull requests

8 participants