Skip to content

Commit 8459ceb

Browse files
authored
chore: migration to phpseclib 3.x (#10)
fix: unit tests
1 parent d582dc0 commit 8459ceb

File tree

12 files changed

+82
-20
lines changed

12 files changed

+82
-20
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^8.2",
19-
"phpseclib/phpseclib": "2.0.47"
18+
"php": "^8.3",
19+
"phpseclib/phpseclib": "3.0.43",
20+
"phpseclib/phpseclib2_compat": "1.0.6"
2021
},
2122
"suggest":{
2223
"lib-openssl": "Required to use AES algorithms (except AES GCM)",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ run following commands on root folder
3030
* curl -s https://getcomposer.org/installer | php
3131
* php composer.phar install --prefer-dist
3232
* php composer.phar dump-autoload --optimize
33-
* phpunit --bootstrap vendor/autoload.php
33+
* vendor/bin/phpunit --bootstrap vendor/autoload.php

src/jwk/impl/AsymmetricJWK.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ public function getX509Url()
187187
*/
188188
protected function checkX509CertMismatch(){
189189
$x509 = $this->getX509LeafCertificate();
190-
return !is_null($x509) && $x509->getPublicKey() !== $this->public_key->getStrippedEncoded();
190+
if(is_null($x509)) return false;
191+
$ppk1 = $x509->getPublicKey();
192+
$ppk2 = $this->public_key->getStrippedEncoded();
193+
return $ppk1 !== $ppk2 ;
191194
}
192195

193196
/**
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace security\rsa;
2+
/**
3+
* Copyright 2025 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use phpseclib3\Crypt\Common\AsymmetricKey;
15+
use phpseclib3\Crypt\RSA;
16+
use phpseclib\Crypt\RSA as RSA_OLD;
17+
18+
/**
19+
* @class CustomPrivateKey
20+
* this is a decorator in order to add getter fpr protected methods
21+
*/
22+
class CustomAsymmetricKey extends RSA
23+
{
24+
25+
protected $key;
26+
public function __construct(AsymmetricKey $key){
27+
parent::__construct();
28+
$this->key = $key;
29+
}
30+
public function getModulus(){
31+
return $this->key->modulus;
32+
}
33+
34+
public function getPrivateExponent(){
35+
return $this->key->exponent;
36+
}
37+
38+
public function getPublicExponent(){
39+
return $this->key->publicExponent;
40+
}
41+
42+
public function toString($type = RSA_OLD::PRIVATE_FORMAT_PKCS8, array $options = [])
43+
{
44+
return $this->key->toString($type, $options);
45+
}
46+
}

src/security/rsa/RSAFacade.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
15+
use phpseclib3\Crypt\PublicKeyLoader;
1416
use security\KeyPair;
1517
use security\rsa\exceptions\RSABadPEMFormat;
1618
use phpseclib\Crypt\RSA;
17-
use phpseclib\Math\BigInteger;
19+
use phpseclib3\Math\BigInteger;
20+
1821
/**
1922
* Class RSAFacade
2023
* @package security\rsa
@@ -65,8 +68,15 @@ public function buildKeyPair($bits){
6568
* @return RSAPublicKey
6669
*/
6770
public function buildPublicKey(BigInteger $n, BigInteger $e){
68-
$public_key_pem = $this->rsa_imp->_convertPublicKey($n, $e);
69-
return new _RSAPublicKeyPEMFormat($public_key_pem);
71+
72+
73+
$key = PublicKeyLoader::load([
74+
'n' => $n,
75+
'e' => $e
76+
]);
77+
78+
$pem = $key->toString('pkcs1');
79+
return new _RSAPublicKeyPEMFormat($pem);
7080
}
7181

7282
/**

src/security/rsa/RSAKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14-
use phpseclib\Math\BigInteger;
14+
use phpseclib3\Math\BigInteger;
1515
/**
1616
* Interface RSAKey
1717
* @package security\rsa

src/security/rsa/RSAPrivateKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
**/
1414
use security\KeyWithPassword;
1515
use security\PrivateKey;
16-
use phpseclib\Math\BigInteger;
16+
use phpseclib3\Math\BigInteger;
1717
/**
1818
* Interface RSAPrivateKey
1919
* @package security\rsa

src/security/rsa/RSAPublicKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* limitations under the License.
1313
**/
1414
use security\PublicKey;
15-
use phpseclib\Math\BigInteger;
15+
use phpseclib3\Math\BigInteger;
1616
/**
1717
* Interface RSAPublicKey
1818
* @package security\rsa

src/security/rsa/_AbstractRSAKeyPEMFormat.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
15+
use phpseclib3\Crypt\PublicKeyLoader;
1416
use security\rsa\exceptions\RSABadPEMFormat;
1517
use phpseclib\Crypt\RSA;
16-
use phpseclib\Math\BigInteger;
18+
use phpseclib3\Math\BigInteger;
1719
/**
1820
* Class _AbstractRSAKeyPEMFormat
1921
* @package security\rsa
@@ -40,6 +42,8 @@ abstract class _AbstractRSAKeyPEMFormat {
4042
*/
4143
protected $password;
4244

45+
protected $key;
46+
4347
/**
4448
* @return null|string
4549
*/
@@ -70,10 +74,8 @@ public function __construct($pem_format, $password = null){
7074
}
7175

7276
$res = $this->rsa_imp->loadKey($this->pem_format, RSA::PRIVATE_FORMAT_PKCS1);
73-
7477
if(!$res) throw new RSABadPEMFormat(sprintf('pem %s',$pem_format ));
75-
76-
$this->n = $this->rsa_imp->modulus;
78+
$this->key = new CustomAsymmetricKey(PublicKeyLoader::load($this->pem_format, $this->password));
7779
}
7880

7981
/**
@@ -82,7 +84,7 @@ public function __construct($pem_format, $password = null){
8284
*/
8385
public function getModulus()
8486
{
85-
return $this->n;
87+
return $this->key->getModulus();
8688
}
8789

8890
}

src/security/rsa/_RSAPrivateKeyPEMFormat.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class _RSAPrivateKeyPEMFormat
3535
public function __construct($pem_format, $password = null){
3636

3737
parent::__construct($pem_format, $password);
38-
$this->d = $this->rsa_imp->exponent;
38+
$this->d = $this->key->getPrivateExponent();
3939
if($this->d->toString() === $this->e->toString())
4040
throw new RSABadPEMFormat(sprintf('pem %s is a public key!', $pem_format));
4141
}
@@ -47,7 +47,7 @@ public function __construct($pem_format, $password = null){
4747
*/
4848
public function getPrivateExponent()
4949
{
50-
return $this->d;
50+
return $this->key->getPrivateExponent();
5151
}
5252

5353
/**

0 commit comments

Comments
 (0)