1717use OCA \Libresign \Exception \LibresignException ;
1818use OCA \Libresign \Exception \SignatureDataNotFoundException ;
1919use 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 ;
2224use OCP \App \IAppManager ;
2325use OCP \Files \AppData \IAppDataFactory ;
2426use 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 )) {
0 commit comments