server icon indicating copy to clipboard operation
server copied to clipboard

Internal Server Error at Login with OpenSSL 1.1.1a openssl_pkey_get_details expects parameter 1 to be resource, boolean given

Open alexacra opened this issue 7 years ago • 10 comments

Summary

I have a Nextcloud instance running on my VPS. With OpenSSL 1.1.1 compiled by source everything ran fine. Yesterday I upgraded OpenSSL 1.1.1 to 1.1.1a and encountered an internal server error, when I tried to login into Nextcloud.

The interesting part here is the following error message:

"Exception":"TypeError","Message":"openssl_pkey_get_details() expects parameter 1 to be resource, boolean given","Code":0,"Trace":[{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php","line":301,"function":"openssl_pkey_get_details","args":[false]

I first reported this bug at the OpenSSL issue page: https://github.com/openssl/openssl/issues/7837

Here's what @mattcaswell said:

The error message suggests that the caller of the PHP function (presumably Nextcloud) is not behaving correctly by passing an invalid value as a parameter. So that suggests a bug in the PHP function caller (presumably Nextcloud?).

Most likely what has happened is that the upgrade to 1.1.1a has suddenly caused some OpenSSL function call to behave differently in some way which has had a knock on impact in Nextcloud - thus triggering the Nextcloud bug and causing it to make the openssl_pkey_new_error function call with an invalid value.

What the reason for the change in OpenSSL behaviour is, is unclear. It could be an intended change in behaviour (e.g. due to a bug fix), or it could be accidental (i.e. a newly introduced bug). Either way the next step would be to isolate the bug in Nextcloud and get that fixed. That should also identify what the underlying change in OpenSSL behaviour was that triggered it.

(Highlighting by me)

Steps to reproduce

  1. Install Nextcloud 14.1
  2. Install OpenSSL 1.1.1a + PHP 7.2.10
  3. Try to login with a valid user

Expected behaviour

Login without any error. :)

Actual behaviour

An error occurs making it impossible to login:

Internal Server Error
The Server could not complete the request.

Server configuration

Operating system: Ubuntu 18.04.1

Web server: Nginx 1.15.7 + OpenSSL 1.1.1a (compiled by source)

Database: mariadb Ver 15.1 Distrib 10.1.34-MariaDB

PHP version: PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02)

Nextcloud version: 14.1

Updated from an older Nextcloud/ownCloud or fresh install: 14.0

Where did you install Nextcloud from: zip-Package from Nextcloud.com

Signing status:

Signing status
All tests pased.

List of activated apps:

App list
Enabled:
  - accessibility: 1.0.1
  - activity: 2.7.0
  - bookmarks: 0.14.2
  - calendar: 1.6.4
  - checksum: 0.4.1
  - cloud_federation_api: 0.0.1
  - comments: 1.4.0
  - contacts: 2.1.8
  - dav: 1.6.0
  - event_update_notification: 0.3.1
  - federatedfilesharing: 1.4.0
  - federation: 1.4.0
  - files: 1.9.0
  - files_pdfviewer: 1.3.2
  - files_sharing: 1.6.2
  - files_texteditor: 2.6.0
  - files_trashbin: 1.4.1
  - files_versions: 1.7.1
  - files_videoplayer: 1.3.0
  - firstrunwizard: 2.3.0
  - gallery: 18.1.0
  - gpxmotion: 0.0.7
  - gpxpod: 2.3.2
  - logreader: 2.0.0
  - lookup_server_connector: 1.2.0
  - nextcloud_announcements: 1.3.0
  - notes: 2.5.1
  - notifications: 2.2.1
  - oauth2: 1.2.1
  - onlyoffice: 2.1.0
  - password_policy: 1.4.0
  - previewgenerator: 2.0.0
  - provisioning_api: 1.4.0
  - serverinfo: 1.4.0
  - sharebymail: 1.4.0
  - spreed: 4.0.1
  - support: 1.0.0
  - survey_client: 1.2.0
  - systemtags: 1.4.0
  - tasks: 0.9.8
  - theming: 1.5.0
  - twofactor_backupcodes: 1.3.1
  - updatenotification: 1.4.1
  - workflowengine: 1.4.0
Disabled:
  - admin_audit
  - bruteforcesettings
  - encryption
  - files_external
  - user_external
  - user_ldap

Nextcloud configuration:

Config report
{
    "system": {
        "instanceid": "***REMOVED SENSITIVE VALUE***",
        "passwordsalt": "***REMOVED SENSITIVE VALUE***",
        "secret": "***REMOVED SENSITIVE VALUE***",
        "trusted_domains": [
            "domain.tld"
        ],
        "datadirectory": "***REMOVED SENSITIVE VALUE***",
        "overwrite.cli.url": "https:\/\/path\/nextcloud",
        "dbtype": "mysql",
        "version": "14.0.4.2",
        "dbname": "***REMOVED SENSITIVE VALUE***",
        "dbhost": "***REMOVED SENSITIVE VALUE***",
        "dbport": "",
        "dbtableprefix": "oc_",
        "dbuser": "***REMOVED SENSITIVE VALUE***",
        "dbpassword": "***REMOVED SENSITIVE VALUE***",
        "memcache.local": "\\OC\\Memcache\\APCu",
        "installed": true,
        "maintenance": false,
        "theme": "",
        "loglevel": 0,
        "updater.release.channel": "stable"
    }
}

Are you using external storage, if yes which one: local

Are you using encryption: no

Are you using an external user-backend, if yes which one: no

Client configuration

Browser: FireFox 63.0.3

Operating system: Manjaro 18

Logs

Nextcloud log (data/nextcloud.log)

{"reqId":"a0FfZr6Dfd2Q7wFQ5t4Q","level":3,"time":"2018-12-05T23:22:41+00:00","remoteAddr":"XXX.XXX.XXX.XXX","user":"USER1","app":"index","method":"POST","url":"\/nextcloud\/login?redirect_url=\/nextcloud\/apps\/files\/","message":{"Exception":"TypeError","Message":"openssl_pkey_get_details() expects parameter 1 to be resource, boolean given","Code":0,"Trace":[{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php","line":301,"function":"openssl_pkey_get_details","args":[false]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php","line":70,"function":"newToken","class":"OC\\Authentication\\Token\\PublicKeyTokenProvider","type":"->","args":["*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/Manager.php","line":69,"function":"generateToken","class":"OC\\Authentication\\Token\\PublicKeyTokenProvider","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/User\/Session.php","line":641,"function":"generateToken","class":"OC\\Authentication\\Token\\Manager","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/core\/Controller\/LoginController.php","line":322,"function":"createSessionToken","class":"OC\\User\\Session","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/Http\/Dispatcher.php","line":166,"function":"tryLogin","class":"OC\\Core\\Controller\\LoginController","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/Http\/Dispatcher.php","line":99,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OC\\Core\\Controller\\LoginController"},"tryLogin"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/App.php","line":118,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OC\\Core\\Controller\\LoginController"},"tryLogin"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/Routing\/RouteActionHandler.php","line":47,"function":"main","class":"OC\\AppFramework\\App","type":"::","args":["OC\\Core\\Controller\\LoginController","tryLogin",{"__class__":"OC\\AppFramework\\DependencyInjection\\DIContainer"},{"_route":"core.login.tryLogin"}]},{"function":"__invoke","class":"OC\\AppFramework\\Routing\\RouteActionHandler","type":"->","args":[{"_route":"core.login.tryLogin"}]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Route\/Router.php","line":297,"function":"call_user_func","args":[{"__class__":"OC\\AppFramework\\Routing\\RouteActionHandler"},{"_route":"core.login.tryLogin"}]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/base.php","line":987,"function":"match","class":"OC\\Route\\Router","type":"->","args":["\/login"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/index.php","line":42,"function":"handleRequest","class":"OC","type":"::","args":[]}],"File":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php","Line":301,"CustomMessage":"--"},"userAgent":"XXX}
{"reqId":"qwr19jSFwzwTGTaBeVGY","level":3,"time":"2018-12-05T23:25:33+00:00","remoteAddr":"XXX.XXX.XXX.XXX","user":"USER1","app":"PHP","method":"POST","url":"\/nextcloud\/login?redirect_url=\/nextcloud\/apps\/files\/","message":"openssl_pkey_export(): cannot get key from parameter 1 at \/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php#298","userAgent":"XXX}
{"reqId":"qwr19jSFwzwTGTaBeVGY","level":3,"time":"2018-12-05T23:25:33+00:00","remoteAddr":"XXX.XXX.XXX.XXX","user":"USER1","app":"index","method":"POST","url":"\/nextcloud\/login?redirect_url=\/nextcloud\/apps\/files\/","message":{"Exception":"TypeError","Message":"openssl_pkey_get_details() expects parameter 1 to be resource, boolean given","Code":0,"Trace":[{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php","line":301,"function":"openssl_pkey_get_details","args":[false]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php","line":70,"function":"newToken","class":"OC\\Authentication\\Token\\PublicKeyTokenProvider","type":"->","args":["*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/Manager.php","line":69,"function":"generateToken","class":"OC\\Authentication\\Token\\PublicKeyTokenProvider","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/User\/Session.php","line":641,"function":"generateToken","class":"OC\\Authentication\\Token\\Manager","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/core\/Controller\/LoginController.php","line":322,"function":"createSessionToken","class":"OC\\User\\Session","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/Http\/Dispatcher.php","line":166,"function":"tryLogin","class":"OC\\Core\\Controller\\LoginController","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/Http\/Dispatcher.php","line":99,"function":"executeController","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OC\\Core\\Controller\\LoginController"},"tryLogin"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/App.php","line":118,"function":"dispatch","class":"OC\\AppFramework\\Http\\Dispatcher","type":"->","args":[{"__class__":"OC\\Core\\Controller\\LoginController"},"tryLogin"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/AppFramework\/Routing\/RouteActionHandler.php","line":47,"function":"main","class":"OC\\AppFramework\\App","type":"::","args":["OC\\Core\\Controller\\LoginController","tryLogin",{"__class__":"OC\\AppFramework\\DependencyInjection\\DIContainer"},{"_route":"core.login.tryLogin"}]},{"function":"__invoke","class":"OC\\AppFramework\\Routing\\RouteActionHandler","type":"->","args":[{"_route":"core.login.tryLogin"}]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Route\/Router.php","line":297,"function":"call_user_func","args":[{"__class__":"OC\\AppFramework\\Routing\\RouteActionHandler"},{"_route":"core.login.tryLogin"}]},{"file":"\/var\/www\/domain.tld\/nextcloud\/lib\/base.php","line":987,"function":"match","class":"OC\\Route\\Router","type":"->","args":["\/login"]},{"file":"\/var\/www\/domain.tld\/nextcloud\/index.php","line":42,"function":"handleRequest","class":"OC","type":"::","args":[]}],"File":"\/var\/www\/domain.tld\/nextcloud\/lib\/private\/Authentication\/Token\/PublicKeyTokenProvider.php","Line":301,"CustomMessage":"--"},"userAgent":"XXX}

alexacra avatar Dec 06 '18 15:12 alexacra

Ref https://github.com/nextcloud/server/issues/11227 (did a lot of debugging here but not sure which openssl version) Ref https://github.com/nextcloud/server/issues/11570 (maybe related) Ref https://github.com/nextcloud/server/issues/11438 (is this true for you? pw > 214 bytes?)

kesselb avatar Dec 06 '18 16:12 kesselb

Ref #11227 (did a lot of debugging here but not sure which openssl version)

This user said he uses Ubuntu 16.04.5 (xenial), so if he did not use a custom OpenSSL installation, it should be OpenSSL 1.0.2g.

Edit2: Got it working now. Changed the /etc/ssl/ Permissions. chmod go+rx /etc/ssl/ does the trick.

Unfortunately the root of the issue of #11227 seems to be different to mine. I already had set the right permissions.

Ref #11570 (maybe related)

Maby related.

Ref #11438 (is this true for you? pw > 214 bytes?)

Nope, password is < 50 bytes even if each char is counted as 2 bytes.

alexacra avatar Dec 06 '18 16:12 alexacra

Could you try https://github.com/nextcloud/server/issues/11227#issuecomment-421578286? Output of would be interesting openssl_error_string().

kesselb avatar Dec 06 '18 16:12 kesselb

Could you try #11227 (comment)? Output of would be interesting openssl_error_string().

I created a new file called openssl_test.php with the content

<?php

$config = [
        'digest_alg' => 'sha512',
        'private_key_bits' => 2048,
];

$res = openssl_pkey_new($config);

var_dump($res);
var_dump(openssl_error_string());

Here are the results:

OpenSSL 1.1.1a

sudo -u www-data php openssl_test.php 
bool(false)
string(61) "error:02001002:system library:fopen:No such file or directory"

OpenSSL 1.1.1

resource(4) of type (OpenSSL key)
string(68) "error:0E06D06C:configuration file routines:NCONF_get_string:no value"

Looks like a permission error to me or the requested file does indeed not exist?

alexacra avatar Dec 06 '18 16:12 alexacra

https://github.com/nextcloud/server/issues/11227#issuecomment-421625604 used strace to look whats going on. Could you try this?

kesselb avatar Dec 06 '18 17:12 kesselb

#11227 (comment) used strace to look whats going on. Could you try this?

https://gist.github.com/alexara/0b793ba1203b3abf8d804822db042eaf

alexacra avatar Dec 06 '18 17:12 alexacra

Solved it:

sudo ln -s /etc/ssl/openssl.cnf /usr/local/ssl/openssl.cnf

alexacra avatar Dec 06 '18 17:12 alexacra

Great. I'll try to improve the logging and write some helpful things to nextcloud.log

kesselb avatar Dec 06 '18 17:12 kesselb

Thank you for all your effort! Improving the logging and giving others who run into a similar issue a hint, sounds great!

alexacra avatar Dec 06 '18 17:12 alexacra

Solved it:

sudo ln -s /etc/ssl/openssl.cnf /usr/local/ssl/openssl.cnf

I got the similar problem. I installed my nc 13 on CentOS 6, with OpenSSL 1.1.1a, PHP 7.2.13, Apache 2.4.37.

I compiled OpenSSL with

--prefix=/usr/local/openssl --openssldir=/usr/local/openssl no-ssl2 no-ssl3 no-weak-ssl-ciphers -Wl,-rpath=/usr/local/openssl/lib && make && make install_sw \
&& PATH=/usr/local/openssl/bin:$PATH && echo $PATH && openssl version -a

Instead of trying ln -s /etc/ssl/openssl.cnf /usr/local/ssl/openssl.cnf (cannot find /etc/ssl/openssl.cnf but can locate it at /etc/pki/tls/openssl.cnf ), I put the following script and it worked then. ln -s /etc/pki/tls/openssl.cnf /usr/local/openssl/openssl.cnf

Thank you, @alexara , and all the others who contributed to this issue.

baoang avatar Dec 18 '18 03:12 baoang