MDL-75645 mnet: Stop caching OpenSSLAsymmetricKey keys
commit856b593796646fb15724495fcfb3e84cbbb519dd
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 20 Sep 2022 08:49:28 +0000 (20 10:49 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 18 Oct 2022 12:30:50 +0000 (18 14:30 +0200)
tree222eca915d46f8c3ca78e92a9d6cb37dfc9af5d3
parentf8d28e4ca6e4c32d68b3f31266cf18b1e87fa206
MDL-75645 mnet: Stop caching OpenSSLAsymmetricKey keys

The mnet_environment->keypair array contains the following
elements (and more, just focussing on these):
- keypair_PEM : textual representation of the private key.
- certificate : textual representation of the public key.
- privatekey  : OpenSSLAsymmetricKey representation of the private key,
                generated from keypair_PEM. See get_private_key().
- publickey   : OpenSSLAsymmetricKey representation if the public key,
                generated from certificate. See get_public_key().

The last 2 elements in the array are only used as "caching", to avoid
having to call to openssl_pkey_get_private() and
openssl_pkey_get_public() to convert from the textual representation
to the OpenSSLAsymmetricKey representation that is the one required
by a number of openssl functions.

Problems arrive when, as part of the MNet protocol, the mnet_environment
is serialised, because, since PHP 8.0 those OpenSSLAsymmetricKey objects
aren't serialisable any more.

So, as far as they are only used for internal caching it's perfectly ok
to remove the caching bits and use the openssl_pkey_get_xxx() methods
to calculate them under demand.

The alternative to this would be to implement into the mnet_environment
some custom serialisation, skipping those OpenSSLAsymmetricKey
instances, using __sleep(), the Serializabla interface or __serialize(),
but that seems unnecessary because, as explained above, the uses are
really limited and easily replaceable.

That's what this patch does.
mnet/environment.php
mnet/lib.php