ratchetphp / Pawl

Asynchronous WebSocket client
MIT License
583 stars 84 forks source link

How to authenticate with PEM certificate? #144

Closed T3chTobi closed 2 years ago

T3chTobi commented 2 years ago

I want to convert following Python code into PHP code using pawl. I was able to convert everything, but the authentication with the PEM certificate. How can I authenticate with a PEM cerficiate?

Original Python code

const fs = require('fs');
var WebSocket = require('ws');

ws = new WebSocket('wss://my.url.com', {
  headers: {
    "token": "[TOKEN]"
  },
  ca: [
    fs.readFileSync('certificate.pem')
  ],
});

ws.on('message', function incoming(data) {
  data = JSON.parse(data);
  console.log(data);
});

The problem is the following fragment:

ca: [
    fs.readFileSync('certificate.pem')
],

Converted PHP code

\Ratchet\Client\connect('wss://my.url.com', [], [
    'token' => '[token]'
])->then(function($conn) {
    $conn->on('message', function($msg) use ($conn) {
        echo "Received: {$msg}\n";
        $conn->close();
    });

    //$conn->send('Hello World!');
}, function ($e) {
    echo "Could not connect: {$e->getMessage()}\n";
});

Error message:

Could not connect: Connection to tls://my.url.com failed during TLS handshake: SSL operation failed with code 1. OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
SimonFrings commented 2 years ago

Hey @T3chTobi, did you found an answer for this, if so, can you share it in here so other users with the same question can find it 👍

T3chTobi commented 2 years ago

https://github.com/ratchetphp/Pawl/issues/53#issuecomment-800522810

That snippet worked