I've created a C# SSL TCP webserver primarily using the example from the SslStream docs, and I've created and downloaded an SSL Certificate (with sslforfree) and created pkcs#7/p7b and pkcs#12 (with a blank password) certs using openssl through powershell. I have found no way to use pkcs# 12 or 7 yet.
(using OpenSSL.X509Certificate2Provider and System.Security.Cryptography.X509Certificates)
The most success was using this code:
string certfiletext = File.ReadAllText(Path.Combine(workingpath, "certificate.crt"));
string privatekeytext = File.ReadAllText(Path.Combine(workingpath, "private.key"));
ICertificateProvider provider = new
CertificateFromFileProvider(certfiletext, privatekeytext);
serverCertificate = provider.Certificate;
This code gets past the sslStream.AuthenticateAsServer requirement, but fails at bytes = sslStream.Read(buffer, 0, buffer.Length);
The stack trace associated with this failure expresses that The decryption operation failed, see inner exception, with inner exception being: Win32Exception: An unknown error occurred while processing the certificate.
This leads me to believe the certificate isn't created properly. The other method I've gotten anywhere with is this:
serverCertificate =
new X509Certificate2(Path.Combine(workingpath, "certificate.pfx"), "");
but that causes the same issue as the prior code.(Decryption error due to processing certificate error). This error appears whether the certificate has been installed on my machine or not. There is also no issue with the server finding the correct files.
This is the code I use for a JS client side embedded in html:
wsUri = "wss://thisismyip:11000",
websocket = new WebSocket(wsUri);
websocket.onopen = function (e) {
websocket.send(window.location.hostname + window.location.pathname.substring(1));
};
websocket.onclose = function (e) {};
websocket.onmessage = function (e) {
//server response, do stuff when it responds
};
websocket.onerror = function (e) {};
While I doubt the issue is related to the JS code, I might have missed something important.
Summary: I can't create an SSL connection between a javascript websocket and a C# TCP server due to a server-side certificate issue.