2222use OCP \ICacheFactory ;
2323use OCP \IConfig ;
2424use OCP \ServerVersion ;
25- use phpseclib \Crypt \RSA ;
26- use phpseclib \File \X509 ;
25+ use phpseclib3 \Crypt \RSA ;
26+ use phpseclib3 \File \X509 ;
2727
2828/**
2929 * Class Checker handles the code signing using X.509 and RSA. ownCloud ships with
@@ -167,24 +167,26 @@ private function generateHashes(\RecursiveIteratorIterator $iterator,
167167 *
168168 * @param array $hashes
169169 * @param X509 $certificate
170- * @param RSA $privateKey
170+ * @param RSA\PrivateKey $privateKey
171171 * @return array
172172 */
173- private function createSignatureData (array $ hashes ,
173+ private function createSignatureData (
174+ array $ hashes ,
174175 X509 $ certificate ,
175- RSA $ privateKey ): array {
176+ RSA \PrivateKey $ privateKey ,
177+ ): array {
176178 ksort ($ hashes );
177179
178- $ privateKey-> setSignatureMode ( RSA :: SIGNATURE_PSS );
179- $ privateKey -> setMGFHash ( ' sha512 ' );
180- // See https://tools.ietf.org/html/rfc3447#page-38
181- $ privateKey -> setSaltLength (0 );
182- $ signature = $ privateKey ->sign (json_encode ($ hashes ));
180+ $ signature = $ privateKey
181+ -> withPadding ( RSA :: SIGNATURE_PSS )
182+ -> withMGFHash ( ' sha512 ' )
183+ -> withSaltLength (0 )
184+ ->sign (json_encode ($ hashes ));
183185
184186 return [
185187 'hashes ' => $ hashes ,
186188 'signature ' => base64_encode ($ signature ),
187- 'certificate ' => $ certificate ->saveX509 ($ certificate ->currentCert ),
189+ 'certificate ' => $ certificate ->saveX509 ($ certificate ->getCurrentCert () ),
188190 ];
189191 }
190192
@@ -193,12 +195,12 @@ private function createSignatureData(array $hashes,
193195 *
194196 * @param string $path
195197 * @param X509 $certificate
196- * @param RSA $privateKey
198+ * @param RSA\PrivateKey $privateKey
197199 * @throws \Exception
198200 */
199201 public function writeAppSignature ($ path ,
200202 X509 $ certificate ,
201- RSA $ privateKey ) {
203+ RSA \ PrivateKey $ privateKey ) {
202204 $ appInfoDir = $ path . '/appinfo ' ;
203205 try {
204206 $ this ->fileAccessHelper ->assertDirectoryExists ($ appInfoDir );
@@ -222,12 +224,12 @@ public function writeAppSignature($path,
222224 * Write the signature of core
223225 *
224226 * @param X509 $certificate
225- * @param RSA $rsa
227+ * @param RSA\PrivateKey $rsa
226228 * @param string $path
227229 * @throws \Exception
228230 */
229231 public function writeCoreSignature (X509 $ certificate ,
230- RSA $ rsa ,
232+ RSA \ PrivateKey $ rsa ,
231233 $ path ) {
232234 $ coreDir = $ path . '/core ' ;
233235 try {
@@ -291,15 +293,14 @@ private function verify(string $signaturePath, string $basePath, string $certifi
291293 $ certificate = $ signatureData ['certificate ' ];
292294
293295 // Check if certificate is signed by Nextcloud Root Authority
294- $ x509 = new \ phpseclib \ File \ X509 ();
296+ $ x509 = new X509 ();
295297 $ rootCertificatePublicKey = $ this ->fileAccessHelper ->file_get_contents ($ this ->environmentHelper ->getServerRoot () . '/resources/codesigning/root.crt ' );
296298
297299 $ rootCerts = $ this ->splitCerts ($ rootCertificatePublicKey );
298300 foreach ($ rootCerts as $ rootCert ) {
299301 $ x509 ->loadCA ($ rootCert );
300302 }
301- $ x509 ->loadX509 ($ certificate );
302- if (!$ x509 ->validateSignature ()) {
303+ if ($ x509 ->loadX509 ($ certificate ) === false || !$ x509 ->validateSignature ()) {
303304 throw new InvalidSignatureException ('Certificate is not valid. ' );
304305 }
305306 // Verify if certificate has proper CN. "core" CN is always trusted.
@@ -310,13 +311,18 @@ private function verify(string $signaturePath, string $basePath, string $certifi
310311 }
311312
312313 // Check if the signature of the files is valid
313- $ rsa = new \phpseclib \Crypt \RSA ();
314- $ rsa ->loadKey ($ x509 ->currentCert ['tbsCertificate ' ]['subjectPublicKeyInfo ' ]['subjectPublicKey ' ]);
315- $ rsa ->setSignatureMode (RSA ::SIGNATURE_PSS );
316- $ rsa ->setMGFHash ('sha512 ' );
317- // See https://tools.ietf.org/html/rfc3447#page-38
318- $ rsa ->setSaltLength (0 );
319- if (!$ rsa ->verify (json_encode ($ expectedHashes ), $ signature )) {
314+ /** @var RSA\PublicKey|false */
315+ $ rsa = $ x509 ->getPublicKey ();
316+ if ($ rsa === false ) {
317+ throw new InvalidSignatureException ('Certificate does not provide valid public RSA key. ' );
318+ }
319+
320+ $ rsa = $ rsa
321+ ->withPadding (RSA ::SIGNATURE_PSS )
322+ ->withMGFHash ('sha512 ' )
323+ ->withSaltLength (0 );
324+
325+ if (!$ rsa ->verify (json_encode ($ expectedHashes ), (string )$ signature )) {
320326 throw new InvalidSignatureException ('Signature could not get verified. ' );
321327 }
322328
0 commit comments