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

feat: add support for phpseclib3 #5251

Merged
merged 6 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Core/src/Testing/KeyPairGenerateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
namespace Google\Cloud\Core\Testing;

use Google\Cloud\Storage\EncryptionTrait;
use phpseclib\Crypt\RSA;
use phpseclib\Crypt\RSA as RSA2;
use phpseclib3\Crypt\RSA as RSA3;

/**
* Trait KeyPairGenerateTrait implements key pair generation functions used for testing
Expand All @@ -32,8 +33,16 @@ trait KeyPairGenerateTrait

private function getKeyPair()
{
$rsa = new RSA;
$rsa->setSignatureMode(RSA::SIGNATURE_PKCS1);
if (class_exists(RSA3::class)) {
$key = RSA3::createKey();
$key = $key->withPadding(RSA3::SIGNATURE_PKCS1)
->withHash('sha256');

return [$key->toString('PKCS1'), $key->getPublicKey()];
}

$rsa = new RSA2;
$rsa->setSignatureMode(RSA2::SIGNATURE_PKCS1);
$rsa->setHash('sha256');

$key = $rsa->createKey();
Expand Down
2 changes: 1 addition & 1 deletion Storage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"squizlabs/php_codesniffer": "2.*",
"phpdocumentor/reflection": "^3.0",
"erusev/parsedown": "^1.6",
"phpseclib/phpseclib": "^2",
"phpseclib/phpseclib": "^2.0||^3.0",
"google/cloud-pubsub": "^1.0"
},
"suggest": {
Expand Down
15 changes: 11 additions & 4 deletions Storage/src/EncryptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

namespace Google\Cloud\Storage;

use phpseclib\Crypt\RSA;
use phpseclib\Crypt\RSA as RSA2;
use phpseclib3\Crypt\RSA as RSA3;

/**
* Trait which provides helper methods for customer-supplied encryption.
Expand Down Expand Up @@ -127,10 +128,16 @@ protected function signString($privateKey, $data, $forceOpenssl = false)
{
$signature = '';

if (class_exists(RSA::class) && !$forceOpenssl) {
$rsa = new RSA;
if (class_exists(RSA3::class) && !$forceOpenssl) {
$rsa = RSA3::loadPrivateKey($privateKey);
$rsa = $rsa->withPadding(RSA3::SIGNATURE_PKCS1)
->withHash('sha256');

$signature = $rsa->sign($data);
} elseif (class_exists(RSA2::class) && !$forceOpenssl) {
$rsa = new RSA2;
$rsa->loadKey($privateKey);
$rsa->setSignatureMode(RSA::SIGNATURE_PKCS1);
$rsa->setSignatureMode(RSA2::SIGNATURE_PKCS1);
$rsa->setHash('sha256');

$signature = $rsa->sign($data);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"erusev/parsedown": "^1.6",
"vierbergenlars/php-semver": "^3.0",
"symfony/lock": "3.3.x-dev#1ba6ac9",
"phpseclib/phpseclib": "^2",
"phpseclib/phpseclib": "^2.0||^3.0",
"google/cloud-tools": "^0.12.0",
"opis/closure": "^3.0",
"swaggest/json-schema": "^0.12.0",
Expand Down