Skip to content

Commit d5c0ace

Browse files
committed
chore: update to phpseclib3
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent f2d5f33 commit d5c0ace

2 files changed

Lines changed: 29 additions & 50 deletions

File tree

lib/Service/Install/SignSetupService.php

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
use OCA\Libresign\Exception\LibresignException;
1818
use OCA\Libresign\Exception\SignatureDataNotFoundException;
1919
use OCA\Libresign\Handler\CertificateEngine\CertificateHelper;
20-
use OCA\Libresign\Vendor\phpseclib\Crypt\RSA;
21-
use OCA\Libresign\Vendor\phpseclib\File\X509;
20+
use OCA\Libresign\Vendor\phpseclib3\Crypt\RSA\PrivateKey;
21+
use OCA\Libresign\Vendor\phpseclib3\Crypt\PublicKeyLoader;
22+
use OCA\Libresign\Vendor\phpseclib3\Crypt\RSA;
23+
use OCA\Libresign\Vendor\phpseclib3\File\X509;
2224
use OCP\App\IAppManager;
2325
use OCP\Files\AppData\IAppDataFactory;
2426
use OCP\Files\IAppData;
@@ -40,7 +42,7 @@ class SignSetupService {
4042
private bool $willUseLocalCert = false;
4143
private string $distro = '';
4244
private ?X509 $x509 = null;
43-
private ?RSA $rsa = null;
45+
private ?PrivateKey $privateKey = null;
4446
private string $instanceId;
4547
private IAppData $appData;
4648
public function __construct(
@@ -74,8 +76,8 @@ public function getArchitectures(): array {
7476
return $appInfo['dependencies']['architecture'];
7577
}
7678

77-
public function setPrivateKey(RSA $privateKey): void {
78-
$this->rsa = $privateKey;
79+
public function setPrivateKey(PrivateKey $privateKey): void {
80+
$this->privateKey = $privateKey;
7981
}
8082

8183
public function setCertificate(x509 $x509): void {
@@ -86,17 +88,19 @@ public function willUseLocalCert(bool $willUseLocalCert): void {
8688
$this->willUseLocalCert = $willUseLocalCert;
8789
}
8890

89-
private function getPrivateKey(): RSA {
90-
if (!$this->rsa instanceof RSA) {
91+
private function getPrivateKey(): PrivateKey {
92+
if (!$this->privateKey instanceof PrivateKey) {
9193
if (file_exists(__DIR__ . '/../../../build/tools/certificates/local/libresign.key')) {
9294
$privateKey = file_get_contents(__DIR__ . '/../../../build/tools/certificates/local/libresign.key');
93-
$this->rsa = new RSA();
94-
$this->rsa->loadKey($privateKey);
95+
$this->privateKey = PublicKeyLoader::loadPrivateKey($privateKey);
9596
} else {
9697
$this->getDevelopCert();
9798
}
9899
}
99-
return $this->rsa;
100+
if (!$this->privateKey instanceof PrivateKey) {
101+
throw new LibresignException('Private key not found');
102+
}
103+
return $this->privateKey;
100104
}
101105

102106
private function getCertificate(): X509 {
@@ -110,6 +114,9 @@ private function getCertificate(): X509 {
110114
$this->getDevelopCert();
111115
}
112116
}
117+
if (!$this->x509 instanceof x509) {
118+
throw new LibresignException('Certificate not found');
119+
}
113120
return $this->x509;
114121
}
115122

@@ -381,12 +388,11 @@ private function validateIfIssignedByLibresignAppCertificate(array $expectedHash
381388
$x509 = $this->getLibresignAppCertificate();
382389

383390
// Check if the signature of the files is valid
384-
$rsa = new RSA();
385-
$rsa->loadKey($x509->currentCert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey']);
386-
$rsa->setSignatureMode(RSA::SIGNATURE_PSS);
387-
$rsa->setMGFHash('sha512');
391+
$rsa = $x509->getPublicKey();
392+
$rsa->withPadding(RSA::SIGNATURE_PSS);
393+
$rsa->withMGFHash('sha512');
388394
// See https://tools.ietf.org/html/rfc3447#page-38
389-
$rsa->setSaltLength(0);
395+
$rsa->withSaltLength(0);
390396

391397
$signatureData = $this->getSignatureData();
392398
$signature = base64_decode((string)$signatureData['signature']);
@@ -525,16 +531,16 @@ private function isExcluded(string $filename): bool {
525531
private function createSignatureData(array $hashes): array {
526532
ksort($hashes);
527533

528-
$this->getPrivateKey()->setSignatureMode(RSA::SIGNATURE_PSS);
529-
$this->getPrivateKey()->setMGFHash('sha512');
530-
// See https://tools.ietf.org/html/rfc3447#page-38
531-
$this->getPrivateKey()->setSaltLength(0);
532-
$signature = $this->getPrivateKey()->sign(json_encode($hashes));
534+
$privateKey = $this->getPrivateKey();
535+
$privateKey->withPadding(RSA::SIGNATURE_PSS);
536+
$privateKey->withMGFHash('sha512');
537+
$privateKey->withSaltLength(0);
538+
$signature = $privateKey->sign(json_encode($hashes));
533539

534540
return [
535541
'hashes' => $hashes,
536542
'signature' => base64_encode($signature),
537-
'certificate' => $this->getCertificate()->saveX509($this->getCertificate()->currentCert),
543+
'certificate' => $this->getCertificate()->saveX509($this->getCertificate()->getCurrentCert()),
538544
];
539545
}
540546

@@ -574,11 +580,10 @@ public function getDevelopCert(): array {
574580
openssl_x509_export($x509, $rootCertificate);
575581
openssl_pkey_export($privateKey, $privateKeyCert);
576582

577-
$this->rsa = new RSA();
578-
$this->rsa->loadKey($privateKeyCert);
583+
$this->privateKey = RSA::loadPrivateKey($privateKeyCert);
579584
$this->x509 = new X509();
580585
$this->x509->loadX509($rootCertificate);
581-
$this->x509->setPrivateKey($this->rsa);
586+
$this->x509->setPrivateKey($this->privateKey);
582587

583588
$rootCertPath = __DIR__ . '/../../../build/tools/certificates/local/';
584589
if (!is_dir($rootCertPath)) {

tests/psalm-baseline.xml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,6 @@
138138
<code><![CDATA[Process]]></code>
139139
</UndefinedClass>
140140
</file>
141-
<file src="lib/Service/Install/SignSetupService.php">
142-
<UndefinedClass>
143-
<code><![CDATA[$this->getCertificate()]]></code>
144-
<code><![CDATA[$this->getCertificate()->currentCert]]></code>
145-
<code><![CDATA[$this->getPrivateKey()]]></code>
146-
<code><![CDATA[$this->getPrivateKey()]]></code>
147-
<code><![CDATA[$this->getPrivateKey()]]></code>
148-
<code><![CDATA[$this->getPrivateKey()]]></code>
149-
<code><![CDATA[$x509->currentCert]]></code>
150-
<code><![CDATA[?RSA]]></code>
151-
<code><![CDATA[?X509]]></code>
152-
<code><![CDATA[RSA]]></code>
153-
<code><![CDATA[RSA]]></code>
154-
<code><![CDATA[RSA]]></code>
155-
<code><![CDATA[RSA]]></code>
156-
<code><![CDATA[RSA]]></code>
157-
<code><![CDATA[RSA]]></code>
158-
<code><![CDATA[RSA]]></code>
159-
<code><![CDATA[X509]]></code>
160-
<code><![CDATA[X509]]></code>
161-
<code><![CDATA[X509]]></code>
162-
<code><![CDATA[X509]]></code>
163-
<code><![CDATA[x509]]></code>
164-
<code><![CDATA[x509]]></code>
165-
</UndefinedClass>
166-
</file>
167141
<file src="lib/Service/RequestSignatureService.php">
168142
<UndefinedClass>
169143
<code><![CDATA[UUIDUtil]]></code>

0 commit comments

Comments
 (0)