Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Notification Crypto error

I use Push Notifs in my PHP Laravel app. I created a pem file and tested it. When using it on my dev machine, it correctly pushes to the mobile device. When I now push the whole project to my production server, and start the pushnotif call, I get the error: Failed to enable crypto

Do I need to create a special pem file while being on production server? And I am not talking about "production certificates" I still want to use the sandbox for testing

<?php

namespace App\Helpers;
use Carbon\Carbon;
use Illuminate\Support\Facades\Config;

class PushNotificationHelper {
    public function pushToResponder($deviceToken) {
        // Set device token of mobile device and the passphrase for certification
        $pushToken = $deviceToken;
        $passphrase = Config::get('MFConfig.PushNotificationTest.passPhrase');
        $APNS = Config::get('MFConfig.PushNotificationTest.APNS');

        // Open new context for streaming and set certificate as well as passphrase
        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', '../App/Certificates/ck.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
        stream_context_set_option($ctx, 'ssl', 'cafile', '../App/Certificates/entrust_2048_ca.cer');

        // Open connection to APNS
        $fp = stream_socket_client($APNS, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

        // If no connection could be made then fail with error, otherwise connect
        if (!$fp) {
            exit("Failed to connect: $err, $errstr" . PHP_EOL);
        }

        echo 'Connected to APNS' . PHP_EOL;

        // Create the payload body
        $body['aps'] = array(
            'alert' => "MEDIFAKTOR Einsatz",
            'sound' => 'default'
        );

        // Encode the payload as JSON
        $payload = json_encode($body);

        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $pushToken) . pack('n', strlen($payload)) . $payload;

        // Send to server
        $result = fwrite($fp, $msg, strlen($msg));

        // If no result is available, the message was not delivered.
        if(!$result) {
            echo 'Message not delivered.' . PHP_EOL;
        } else {
            echo 'Message successfully delivered.' . PHP_EOL;
        }

        // Close connection
        fclose($fp);

    }
}

I tested the connection with:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert ck.pem -debug -showcerts -CAfile entrust_2048_ca.cer

and it returns status 0 (ok) which is fine I think? but when I call the function I get: failed loading cafile stream

like image 925
sesc360 Avatar asked Jun 27 '15 16:06

sesc360


1 Answers

Not sure about this, but it might be your php settings in the dev enviroment.

try to locate the cacert.pem file.

If you are using a linux system you can try locate cacert.pem using the terminal.

When you find the location find the php.ini file and find this line:

;openssl.cafile =

and change this to openssl.cafile= path from locate cacert.pem command or the location the .pem is actually located.

Report back on how it went.

like image 109
Harry Geo Avatar answered Oct 16 '22 03:10

Harry Geo