4

I am cloning a repo of a Laravel project. I clone the repo and do php artisan key: generate which correctly sets the keys in .env. However when the app makes an external API request, it fails with the error

openssl_seal(): not a public key (1th member of pubkeys)

I have checked and the env('API_KEY') and 'API_URL' variables are correct.

public static function request(string $url, $data = []): Response
{
    //This is where the code fails
    $encrypt_result = self::encrypt($data, env('API_KEY'));

    return Http::asForm()->post(env('API_URL') . $url, [
        'key' => env('API_KEY'),
        'hash' => $encrypt_result['hash'],
        'e_key' => $encrypt_result['key']
    ]);
}

public static function encrypt(array $data, string $public_key): array
{
    //This is where the code fails
    openssl_seal(json_encode($data), $encrypted, $e_keys, [base64_decode($public_key)]);

    return [
        'hash' => base64_encode($encrypted),
        'key' => base64_encode($e_keys[0])
    ];
}

I have previously seen questions that attribute this to an error with XAMPP, and I was using XAMPP on Windows though have subsequently replaced it with Wampserver and the issue persists.

While using Linux on this project fulltime is not viable, I will be testing it later.

Is there anything simple I might be overlooking?

Josh
  • 186
  • 1
  • 3
  • 17
  • which PHP Version you are running? – Maik Lowrey Mar 01 '22 at 12:42
  • I’m running PHP 7.3. The repo *should* also be 7.3 – Josh Mar 01 '22 at 13:57
  • Thanks for answer. It is possible that you use a higher PHP Version for your project. Like 8 or higher? I once read with a similar problem that PHP had problems up to a certain 7.x version and then it was fixed. – Maik Lowrey Mar 01 '22 at 14:27
  • The error persists on PHP 8.0 and 8.1 – Josh Mar 01 '22 at 14:46
  • @JohnDredge I'm curious if you also get the problem under linux. that could narrow down the problem well. – Maik Lowrey Mar 01 '22 at 14:50
  • @JoshDredge - was it working before? If not are you sure that `[base64_decode($public_key)]` is a correct public key? Check [this example in PHP documentation](https://www.php.net/manual/en/function.openssl-seal.php#refsect1-function.openssl-seal-examples) on how to read the public key. – piotr.gradzinski Mar 02 '22 at 20:15
  • @piotr.gradzinski The problem is now confirmed windows only. The code works fine on Linux so I will troubleshoot this further. The key directly comes from the .env file and is the same on both installations. – Josh Mar 03 '22 at 06:01
  • May be this helps [Issue with Openssl](https://stackoverflow.com/a/11427989/2324206) – Haridarshan Mar 07 '22 at 12:23
  • The solution referenced is out of date and I’ve tried it, the DLLs no longer exist. And this persists on WAMPserver and other non-XAMPP servers too. It is not strictly a code issue as the project works perfectly fine on Linux. – Josh Mar 07 '22 at 12:58
  • Why not use Docker?? – Arshad Ali Mar 08 '22 at 09:17
  • I might have to. It's not used anywhere else in the company. Feels like a band-aid fix to just creating a truly cross-platform solution. – Josh Mar 08 '22 at 23:06

1 Answers1

0

The problem seems to be a Windows problem. Since you said it would run on Linux, I would move the application into a Linux docker container. You can containerize existing Laravel projects in a few steps. With Laravel Sail. The documentation explains it very well: https://laravel.com/docs/8.x/sail.

If there is no Sail in your composer.json, add it to composer require laravel/sail --dev.

But if you are familiar with docker-compose then just use docker plain without Laravel Sail.

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • Is it possible to containerize my local version of the application without effecting the repo? – Josh Mar 09 '22 at 23:13
  • @JoshDredge Theoretically and practically it is possible. In minimum Laravel 8 comes with sail by default. Check out your composer.json. Open a new testbranch and run `php artisan sail:install`. Would be nice to get feedback if it works! If you have question ping me! – Maik Lowrey Mar 10 '22 at 21:14