SSL context options for in memory cert and pk
Description
It is dangerous to store certificates and privateKeys (without a password) on the disk. Unfortunately, this is needed to load the * .p12 file into stream_context_create.
Please, add the option to set certificates and privateKeys from string content or at least from Data URLs, instead the file path only.
// Using string content
// --------------------
openssl_pkcs12_read(file_get_contents('cert.p12'),$certificates,'pass');
$stream_context = stream_context_create(
[ 'ssl' => [ 'local_cert' => $certificates['cert'],
'local_pk' => $certificates['pkey']
]
]);
// Using Data URLs
// ---------------
openssl_pkcs12_read(file_get_contents('cert.p12'),$certificates,'pass');
$stream_context = stream_context_create(
[ 'ssl' => [ 'local_cert' => 'data:,'.$certificates['cert'],
'local_pk' => 'data:,'.$certificates['pkey']
]
]);
// Hack: temp files
// ----------------
openssl_pkcs12_read(file_get_contents('cert.p12'),$certificates,'pass');
file_put_contents('cert.temp',$certificates['cert']);
file_put_contents('pkey.temp',$certificates['pkey']);
$stream_context = stream_context_create(
[ 'ssl' => [ 'local_cert' => 'cert.temp',
'local_pk' => 'pkey.temp'
]
]);
Expected result: Reading certificates and privateKeys without temp files. Actual result: Reading certificates and privateKeys only with temp files.
PHP Version
PHP 8.1.1
Operating System
All
This doesn't look like a bug; it's more a feature request, and makes sense as such.
This is actually addressed by this PR: https://github.com/php/php-src/pull/6860 that adds support for PKCS#11 - It's kind of a side effect of those changes. However the fact that it's using engines and there's no PKCS#11 provider is causing the PR is currently being delayed. If that doesn't change for some time (meaning if there's no provider for long time), we might consider extracting those changes.
Things are delayed for years because the next feature is expected (aka "providers"). Even libp11 is not there yet https://github.com/OpenSC/libp11/issues/432#issue-1063450255
I do not want to rework these topics, I spent too much time, I moved to something else, but I feel it is the "Holy Grail". It would be more pragmatic to use engines since it is a well understood practice and then to add/extend with provider.
any news? https://github.com/OpenSC/libp11/issues/432#issue-1063450255 seem closed
I do not understand the status on libp11/openssl 3.0. Maybe @dengert can provide a proper summary.
Then, as I stated, my proposal was properly set using engines. Anyone is welcomed to update it using providers.
I do not understand the status on libp11/openssl 3.0. Maybe @dengert can provide a proper summary.
It looks like RedHat i.e. https://github.com/latchset/pkcs11-provider and @simo5 are far along with a pkcs11 provide for OpenSSL.
IMHO, it looks like libp11's engine will be replaced by pkcs11-provider. This should still allow the use of libp11 to be called directly. This may require libp11 to be built without the engine. Originally libp11 was separate from the opensc-engine.
Also see https://github.com/OpenSC/libp11/issues/432 The libp11 engine requires libp11 which makes all the pkcs11 calls. The https://github.com/latchset/pkcs11-provider is calling pkcs11 directly. The question is there something that libp11/engine can do that pkcs11-provider can not do.
Also see OpenSC/libp11#432 The libp11 engine requires libp11 which makes all the pkcs11 calls. The https://github.com/latchset/pkcs11-provider is calling pkcs11 directly. The question is there something that libp11/engine can do that pkcs11-provider can not do.
You can try https://github.com/php/php-src/pull/6860#issuecomment-855297541 and check. As stated, I gave up this thread. Waiting 1y+ to get providers was not an option for me.
I am not sure how got involved in this issue. I have been involved with smartcards, OpenSSL (when it was "ssleay") for over 20 year. And PIV, OpenSC and libp11 since 2004. I retired 8 years ago An OpenSSL provider for PKCS11 is a big project. https://github.com/OpenSC/libp11/issues/432 was closed by @mtrojnar IFAIK RedHat appears to be the only one working on a provider with enough manpower to pull it off, so I am following it closely.
Thanks for the context. So who could we ping ?
Thanks for the context. So who could we ping ?
https://github.com/orgs/OpenSC/teams/opensc-maintainers would be a good start. @mtrojnar is the lead maintainer, and I have not seen anything from him on libp11 since Aug 17. (search filter involves:mtrojnar)
@mtrojnar are you reading this?
What appears to be going on:
- People are looking for libp11 to work with OpenSSL 3.0+
- @mtrojnar stated he believed OpenSSL was going to have a pkcs11 provider
- No word from OpenSSL about a provider in the near future
- @simo5 started to write a provider that would use libp11
- @simo5 gave up that effort because libp11 needed too many changes
- @simo5 started https://github.com/latchset/pkcs11-provider from scratch
- libp11 at one time could be useful without a pkcs11 engine
- The pkcs11-engine depends on libp11
- libp11 and its pkcs11-engine are now in the same package
- If it was possible to build libp11 without its engine would that help in short or long term?
- libp11 is using deprecated functions
- no attempt to update libp11 to use supported functions
- without changes libp11 package will eventually be unusable with OpenSSL 3.0+
So questions I would ask:
- Is libp11 used without the engine by anyone?
- Is libp11 only used by the engine?
- Will https://github.com/latchset/pkcs11-provider replace the pkcs11-engine?
@vjardinMisc As a user of libp11 and/or its engine, you could open an issue on libp11 stating what your need. The https://github.com/orgs/OpenSC/teams also lists all the OpenSC teams, and if needed you could also contact the core members if you fell the libp11 team need help and volunteer to be on the team.
You will also note that https://github.com/latchset/pkcs11-provider has a number of contributors that are OpenSC members and contributors.
Yes, I'm reading this thread.
First, I'll answer a few questions apparently unrelated to the main topic of this issue:
Is libp11 used without the engine by anyone?
Yes, although most likely only for keys issued by a private/internal root CA.
Is libp11 only used by the engine?
No. A public example https://github.com/OpenSC/pam_p11. Several people also wrote about their private projects using this library.
Will https://github.com/latchset/pkcs11-provider replace the pkcs11-engine?
Yes, I expect people to eventually migrate to a provider one day, and https://github.com/latchset/pkcs11-provider looks like a good candidate. Nevertheless, I don't expect it to happen anytime soon. Possibly, OpenSSL's migration from engines to prividers will take as much time as Microsoft's migration from WIN32 to UWP.
With regards to keeping certificates and unencrypted private keys on a disk:
- Certificates are, by definition, public. I see no risk in keeping them on the same disk where the signed files are kept.
- In case storing unencrypted keys on a disk is considered an unacceptable risk, consider encrypting them or using secure cryptographic devices for private key operations.
@mtrojnar Thanks for the response. IMHO there are two parts to an OpenSSL 3.0 migration: provider replaces engine and requirement to use only EVP_PKEY i.e. non-deprecated routines for access to keys.
OpenSSL and distros are driving the process to get to OpenSSL 3.0 sooner rather then later: I disagree with your: "eventually migrate to a provider one day" and "I don't expect it to happen anytime soon."
I feel there is more of a sense of urgency. @simo5 who works for RedHat, gave up on using libp11 with a new provider because libp11 needed too many changes, and so started to call pkcs11 directly from https://github.com/latchset/pkcs11-provider. @jakuje who alos works for RedHat is one of the OpenSC developers and is working on "latchset"
Where we are headed is the "latchset" code for pkcs11 provider and an older libp11 that that can still be used with older OpenSSL or one of its competitors. Hopefully no one will need to use both in the same process.. "private/internal root CA"?
Would it make sense to have libp11 upgraded to use EVP_PKEY routines and be buildable without an engine so they could both use the same OpenSSL 3.0?
@dengert I really don't want to participate in spamming an unrelated issue with engine vs provider predictions. It’s difficult to make predictions, especially about the future. Also, while you may be right, argumentum ad verecundiam is not the best way to convince me...
Getting back to the topic of supporting pkcs11 uris in PHP, I do not think a new software should be implemented around OpenSSL's deprecated engines anymore as the 1.1 branch will be out of support in less than a year. For future proof solutions, using providers should be considered, regardless if you decide to implement your own, or rewrite the libp11 to provide the providers api. I would recommend trying the pkcs11-provider mentioned previously, because I am working on that right now, but it is not ready for production yet (but we hope we will get there soon). @vjardin If the windows support is your concern in this use case, please have a look into latchset/pkcs11-provider#128. We are Linux users and developers so we primarily target Linux, but any input from people with different use cases are welcomed.
No my need is not windows per se, but that for years we are waiting for a provider while engine could remain during an interim. Since then, I moved to another topic. So we end up with no option.
@vjardinMisc this going nowhere... =/
is posible retaken the PR: https://github.com/php/php-src/pull/6860 meanwhile?
@vjardinMisc this going nowhere... =/
is posible retaken the PR: #6860 meanwhile?
For doing what? @bukka made a clear statement in 2021. It is stalled. Anyone is welcomed to restart the work using provider that will arrive some days.