ZATCA icon indicating copy to clipboard operation
ZATCA copied to clipboard

Fatal Error When Retrieving Private Key

Open AhmedImran007 opened this issue 2 years ago • 1 comments

Thank you so much for your SallaApp.

I'm trying to integrate SallaApp and Zatca Phase 2 into my project using the provided code. However, I encountered an issue when attempting to retrieve the Private Key.

namespace Challan\ZATCA;
use Salla\ZATCA\GenerateCSR;
use Salla\ZATCA\Models\CSRRequest;
use Salla\ZATCA\Helpers\Certificate;
require_once 'functions.php';
require_once   'vendor/autoload.php';
class Challan_Zatca {
    public  $csr = null;
    public  $generateCSR = null;
    public  $csrRequest = null;
    public function __construct() {
        $this->initialize();
    }

    public function initialize() {
        $CSRRequest = new CSRRequest();
        $CSRRequest->setUID('300055184400003');
        $CSRRequest->setSerialNumber('TST', 'TST', 'ed22f1d8-e6a2-1118-9b58-d9a8f11e44yt' );
        $CSRRequest->setCommonName('TST-886431145-300055184400003');
        $CSRRequest->setOrganizationName('Antna Technologies');
        $CSRRequest->setOrganizationalUnitName('Riyadh');
        $CSRRequest->setCountryName('SA');
        $CSRRequest->setInvoiceType('0', '1');
        $CSRRequest->setRegisteredAddress('Riyadh');
        $CSRRequest->setBusinessCategory('Civil');


        $generateCSR = new GenerateCSR($CSRRequest);
        $this->generateCSR = $generateCSR;
        $this->csrRequest = $CSRRequest;
        $generateCSR->initialize();
        $this->csr =  $generateCSR->generate();

    }
}
$challan_zatca = new Challan_Zatca();
$public_key_pem = openssl_pkey_get_private($challan_zatca->csr->getPrivateKey());
$public_key_pem = openssl_pkey_get_details($public_key_pem)['key'];
$certificate = new Certificate($challan_zatca->csr->getCsrContent(), $public_key_pem);
print_r($certificate);




Fatal error:  Uncaught phpseclib3\Exception\NoKeyLoadedException: The key that was loaded was not a private key in X:\xamp8.1\xx\xx\ZATCA\vendor\phpseclib\phpseclib\phpseclib\Crypt\Common\AsymmetricKey.php:185
Stack trace:
#0 X:\xamp8.1\xx\xx\\ZATCA\src\Helpers\Certificate.php(40): phpseclib3\Crypt\Common\AsymmetricKey::loadPrivateKey(Object(phpseclib3\Crypt\EC\PublicKey))
#1 X:\xamp8.1\xx\xx\\ZATCA\Challan_Zatca.php(51): Salla\ZATCA\Helpers\Certificate->__construct('-----BEGIN CERT...', '-----BEGIN PUBL...')
#2 {main}
  thrown in X:\xamp8.1\xx\xx\\ZATCA\vendor\phpseclib\phpseclib\phpseclib\Crypt\Common\AsymmetricKey.php on line 185


I seek your guidance in resolving this issue. Your support in rectifying the problem will be highly appreciated.

Thank you in advance for your assistance.

AhmedImran007 avatar Jan 03 '24 11:01 AhmedImran007

Hello @AhmedImran007

you can not send the second params to Salla\ZATCA\Helpers\Certificate of type resource or OpenSSLAsymmetricKey You must get the private-key Content and send it like this


namespace Challan\ZATCA;
use Salla\ZATCA\GenerateCSR;
use Salla\ZATCA\Models\CSRRequest;
use Salla\ZATCA\Helpers\Certificate;
require_once 'functions.php';
require_once   'vendor/autoload.php';
class Challan_Zatca {
    public  $csr = null;
    public  $generateCSR = null;
    public  $csrRequest = null;
    public function __construct() {
        $this->initialize();
    }

    public function initialize() {
        $CSRRequest = new CSRRequest();
        $CSRRequest->setUID('300055184400003');
        $CSRRequest->setSerialNumber('TST', 'TST', 'ed22f1d8-e6a2-1118-9b58-d9a8f11e44yt' );
        $CSRRequest->setCommonName('TST-886431145-300055184400003');
        $CSRRequest->setOrganizationName('Antna Technologies');
        $CSRRequest->setOrganizationalUnitName('Riyadh');
        $CSRRequest->setCountryName('SA');
        $CSRRequest->setInvoiceType('0', '1');
        $CSRRequest->setRegisteredAddress('Riyadh');
        $CSRRequest->setBusinessCategory('Civil');


        $generateCSR = new GenerateCSR($CSRRequest);
        $this->generateCSR = $generateCSR;
        $this->csrRequest = $CSRRequest;
        $generateCSR->initialize();
        $this->csr =  $generateCSR->generate();

    }
}

$challan_zatca = new Challan_Zatca();

openssl_pkey_export($challan_zatca->csr->getPrivateKey(), $privateKey);

$certificate = new Certificate($challan_zatca->csr->getCsrContent(), $privateKey);
print_r($certificate);

let me see if you get it

thaifanisalla avatar Feb 17 '24 21:02 thaifanisalla