Skip to content

Instantly share code, notes, and snippets.

@ZiggyMaes
Last active April 11, 2019 12:20
Show Gist options
  • Save ZiggyMaes/3686b7e3cbd346b467d4fb5a6696a35c to your computer and use it in GitHub Desktop.
Save ZiggyMaes/3686b7e3cbd346b467d4fb5a6696a35c to your computer and use it in GitHub Desktop.
using OpenSSL.X509Certificate2Provider;
using System;
using System.IO;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using WebSocketSharp;
namespace FcdConsole
{
class Program
{
static void Main(string[] args)
{
try
{
using (var ws = new WebSocket("wss://x-fcd-endpoint.be-mobile.biz/v1/ws"))
{
ws.SslConfiguration.TargetHost = "x-fcd-endpoint.be-mobile.biz";
ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
ws.SslConfiguration.ClientCertificateSelectionCallback += (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) =>
{
//openssl pkcs12 -export -out /mnt/d/temp/real-fcd.pfx -inkey /mnt/d/temp/fcd.key -in /mnt/d/temp/fcd.crt
//fcd.key is your private key used to generate the .csr
//fcd.crt is the crt you got from be-mobile
return new X509Certificate2(@"D:\temp\real-fcd.pfx");
};
ws.SslConfiguration.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
//validate if the thumbprint of the fcd.crt is equal to the be mobile server certificate
if (certificate.GetCertHashString() == new X509Certificate2(@"D:\temp\fcd.crt").Thumbprint)
{
return true;
}
else
{
return sslPolicyErrors == SslPolicyErrors.None;
}
};
ws.OnMessage += (sender, e) => Console.WriteLine("Message: " + e.Data);
ws.OnError += (sender, error) => Console.WriteLine("Error: " + error);
ws.Connect();
ws.Send("{ \"vehicleId\":\"ASCII-Vehicle-ID\", \"vehicleClass\":1,\"timestamp\":1479673407,\"lon\":51.019426,\"lat\":3.768572,\"heading\":270,\"speed\":120.5,\"hdop\":5,\"metadata\": { \"routeNumber\": 62 }}");
}
}
catch (Exception e)
{
Console.Write(e.ToString());
}
Console.ReadKey(true);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="OpenSSL.PrivateKeyDecoder" version="1.3.0" targetFramework="net45" />
<package id="OpenSSL.X509Certificate2.Provider" version="1.3.0" targetFramework="net45" />
<package id="WebSocketSharp" version="1.0.3-rc11" targetFramework="net45" />
</packages>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment