我有一个应用程序,用于SslStream发送和接收具有自己的固定长度框架的数据.通过包装NetworkStream返回来创建流,TcpClient.GetStream()如下所示:
var client = new TcpClient();
client.Connect(host, port);
var sslStream = new SslStream(client.GetStream(), false, callback, null);
sslStream.AuthenticateAsClient(hostname);
Run Code Online (Sandbox Code Playgroud)
因为协议是完全异步的(框架"消息"在任意时间到达并允许客户端在任意时间发送它们),我通常会生成一个负责阻塞的线程,NetworkStream.Read()并确保NetworkStream.Write(...)在任何时候只有一个线程调用一度.
该备注的部分NetworkStream说:
可以在NetworkStream类的实例上同时执行读写操作,而无需同步.只要有一个用于写操作的唯一线程和一个用于读操作的唯一线程,读写线程之间就不会存在交叉干扰,也不需要同步.
但是,MSDN文档 "线程安全"部分SslStream说:
此类型的任何公共静态(在Visual Basic中为Shared)成员都是线程安全的.任何实例成员都不保证是线程安全的.
因为SslStream并且NetworkStream不在同一个类层次结构中,所以我假设(可能不正确)该备注NetworkStream不适用于SslStream.
线程安全的最佳方法是简单地用这样的东西包装SslStream.BeginRead/ SslStream.EndRead和SslStream.BeginWrite/ SslStream.EndWrite吗?
internal sealed class StateObject
{
private readonly ManualResetEvent _done = new ManualResetEvent(false);
public int BytesRead { get; set; }
public ManualResetEvent …Run Code Online (Sandbox Code Playgroud) 我正在创建一个Windows服务,它将调用API.对于此过程,我正在尝试建立Mutual(双向)SSL身份验证.因为我是新手.我试图实现一个简单的客户端和服务器项目,它将相互验证.
我创建了自签名证书,并在可信证书中添加.
我的客户
using System;
using System.Configuration;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
namespace MutualSslDemo.Client
{
class Program
{
static void Main(string[] args)
{
// hostName
var hostName = ConfigurationManager.AppSettings["hostName"];
if (String.IsNullOrEmpty(hostName))
throw new ArgumentNullException("hostName", "Please specify a valid hostname to connect to.");
// port
var port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
if (port <= 0)
throw new ArgumentException("Please specify a valid port number.");
// certificate and password
var certificate = ConfigurationManager.AppSettings["certificate"];
var password = ConfigurationManager.AppSettings["password"];
var certificates = …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个可以接受安全SSL和不安全的纯文本连接的服务器(为了向后兼容).我的代码几乎正常工作,除了从不安全客户端收到的第一个传输数据丢失了服务器上的前5个字节(字符).更具体地说,如果我在不安全的连接上传输30个字节,当服务器到达该OnClientDataReceived()函数时,则行" int iRx = nwStream.EndRead(asyn);",然后iRx = 25.从客户端发送的任何后续消息都包含所有发送的字节/字符.我怀疑连接的初始假设SSLStream可能是剥离前5个字节然后当它失败时,那5个字节已经从缓冲区中提取出来并且不再可用.有没有人知道我可以采用另一种方法来编写代码,以便服务器能够自动切换?
我试图避免做以下事情:
NetworkStream,然后请求升级到SSL流TcpListeners在两个不同的端口上设置两个(一个用于安全,一个用于不安全)/// Each client that connects gets an instance of the ConnectedClient class.
Class Pseudo_ConnectedClient
{
//Properties
byte[] Buffer; //Holds temporary buffer of read bytes from BeginRead()
TcpClient TCPClient; //Reference to the connected client
Socket ClientSocket; //The outer Socket Reference of the connected client
StringBuilder CurrentMessage; //concatenated chunks of data in buffer until we have a complete message (ends with …Run Code Online (Sandbox Code Playgroud) 我尝试通过本教程使用SslStream的 .NET 实现。我按照本文中的方式做了所有事情,但我有一些问题。我下载了 RawCap 并从本地主机捕获了数据包,之后我使用 Wireshark 打开转储文件(.pcap),但我看到的不是 SSL 或 TLS 类型的协议,而是 TCP:
这是 TCP 流的一部分:
....l...h..T.....Y.2..1...t.4....8.J...../.5 ... .... .2.8......'......Ssl服务器。......................M..T...M.<.......eE
...M.%c...Kg.. .....iKku.^q(C.$..;..Mx......g&./. .................0...0.........L.^..F.)...{.0. ..+......0.1 0...U....MyCA0.. 150228143257Z。391231235959Z0.1.0...U....SslServer0.."0 ..*.H...........0.. ......p9.cq...F. ^....Bm...S... .....Bg.B.<]..f[...<...q'..Tc......R... ZCDD..N...1..0.v..l. <...d[=C.7.8.+^..j(...X.........D'. !*5.^.^Ef..=.6y@.=..)|...Sp..Sv.|h.-.. UT..&bG.......J{.7k. X.........:.[o..o.,r..0..Wi.s..8.Jn9........4...WU....fkmw..........D0B0@..U...907..m.....@.. ...f...0.1 0...U.. ..MyCA..(J..j+..K......0...+..h..X.+.M..{...n .....@.'.F........~....@9J.......A4,>.9.DE.R.V4t._bGY.,v.> G......J ....3....@..c,%[S._..u...$....!Vr...~...'.~ ..T*[.#[.?.B{..Z..mp9.7.Uu}.e..c..H^n.:...-AJxs.. ...:... ..u.....YB..2n...yg=...(.Fv...B.Is..GK.....;.F..Ln..... L...ao ......@...q0o1.0...U....SE1.0...U. ..AddTrust AB1&0$..U....AddTrust 外部 TTP 网络1 “0 ..U...AddTrust 外部 CA 根..0..1.0...U....US1.0...U. .GeoTrust Inc.1907..U...0(c) 2008 GeoTrust Inc. - 仅供授权使用1604..U...-GeoTrust 主要证书颁发机构 - G3.g0e1.0...U....US1.0...U。
这是正确的 SSL/TLS 流吗?为什么Wireshark将其标记为TCP?
更新:

我试图SslStream.AuthenticateAsClient通过传递服务器IP,从C#发送APN使用方法,SslProtocols.Tls和X509Certificate2Collection.但我收到一条错误消息:
Authentication failed because remote party has closed the transport stream
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这里讨论的每个解决方案,但没有任 请帮助下面的代码
X509Certificate2Collection certs = new X509Certificate2Collection();
X509Certificate2 xcert = new X509Certificate2();
xcert.Import(@"D:\certify.p12", "password", X509KeyStorageFlags.UserKeySet);
certs.Add(xcert);
// Apple development server address
string apsHost;
if (xcert.ToString().Contains(ProductionKeyFriendName))
apsHost = "gateway.push.apple.com";
else
apsHost = "gateway.sandbox.push.apple.com";
// Create a TCP socket connection to the Apple server on port 2195
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(apsHost, 2195);
// Create a new SSL stream over the connection
sslStream = …Run Code Online (Sandbox Code Playgroud) .net openssl sslstream x509certificate apple-push-notifications
我正在尝试使用 SslStream 和 TLS 1.2 协议建立到远程服务器的 TCP 连接。代码如下:
_tcpClient.Connect(endPoint);
var certificate = new X509Certificate2(_settings.CertificateFilePath, _settings.CertificatePassword, X509KeyStorageFlags.MachineKeySet);
var certificates = new X509CertificateCollection { certificate };
_nStream = _tcpClient.GetStream();
_sslStream = new SslStream(_nStream, false,
(o, x509Certificate, chain, errors) => true,
(o, s, collection, x509Certificate, issuers) =>
{ return collection[0]; }
);
_sslStream.AuthenticateAsClient(_settings.HostIpAddress, certificates, SslProtocols.Tls12, true);
_sslStream.Write(someData, 0, someData.Length);
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了一个例外:
System.Security.Authentication.AuthenticationException:对 SSPI 的调用失败,请参阅内部异常。---> System.ComponentModel.Win32Exception: 处理证书时发生未知错误
--- 内部异常堆栈跟踪结束
在 System.Net.Security.SslState.CheckThrow(Boolean authSucessCheck) 在 System.Net.Security.SslStream.Write(Byte[] buffer, Int32 offset, Int32 count)
我启用了 SChannel 日志记录并在 Windows 事件日志中找到了这个:
远程服务器已请求 …
我正在尝试更新我的应用程序TcpClient以使用 TLSSslStream而不是普通的Stream,我为此使用的代码似乎在 Unity 之外工作,但是当集成到我的 Unity 2019.1.8 中时失败(也在 2018 和 2017 上测试过) ) 项目。
要建立连接并打开一个新连接,SslStream我使用以下代码:
public static void InitClient(string hostName, int port, string certificateName)
{
client = new TcpClient(hostName, port);
if (client.Client.Connected)
{
Debug.LogFormat("Client connected succesfully");
}
else
{
Debug.LogErrorFormat("Client couldn't connect");
return;
}
stream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
stream.AuthenticateAsClient(certificateName);
}
catch (AuthenticationException e)
{
Debug.LogErrorFormat("Error authenticating: {0}", e);
if (e.InnerException != null)
{
Debug.LogErrorFormat("Inner exception: {0}", e); …Run Code Online (Sandbox Code Playgroud) 我有一种情况需要为使用SslStream作为服务器的客户端生成SSL证书.
我知道如何做到这一点(makecert.exe),但我在尝试确保连接的两端都经过身份验证时遇到了问题.
基本上,如果我使用自签名证书,我需要将其添加到受信任的根,以使相互身份验证工作.如果可以,我宁愿避免这样做.我知道我可以购买SSL证书,但我的情况是我需要为每个客户创建一个不同的证书,因此为每个客户购买证书是不可能的.
有没有办法避免这种情况?也许我可以购买一些证书,我可以使用它来生成更多证书?你可能会说,我对SSL一般都很无能为力.
我有这个代码:
string certificateFilePath = @"C:\Users\Administrator\Documents\Certificate.pfx";
string certificateFilePassword = "Some Password Here";
X509Certificate clientCertificate = new X509Certificate(certificateFilePath, certificateFilePassword);
TcpClient client = new TcpClient(host, port);
SslStream stream = new SslStream(client.GetStream(), false, (sender, certificate, chain, errors) => true);
X509CertificateCollection clientCertificates = new X509CertificateCollection {clientCertificate};
stream.AuthenticateAsClient(host, clientCertificates, SslProtocols.Tls, false);
Run Code Online (Sandbox Code Playgroud)
当我在控制台应用程序中运行代码时,一切正常,stream.IsAuthenticated并stream.IsMutuallyAuthenticated返回true并stream.LocalCertificate包含正确的证书对象。
然而,当在 a 中运行完全相同的代码时Windows Service (as LOCAL SYSTEM user),虽然stream.IsAuthenticated返回true,stream.IsMutuallyAuthenticated返回false并stream.LocalCertificate返回null。
在这两种情况下都会发生这种情况,在第一行运行后clientCertificate加载正确的认证数据并包含证书Subject …
我正在使用 .NET Core 3.0 制作仅支持 TLS 1.3 的代理,我看到 SslProtocols 中定义了 Tls13 = 12288。但在我的测试过程中,它只抛出异常“客户端和服务器无法通信,因为它们不具备通用算法”。所以我的问题是.NET Core 3.0 现在是否支持 TLS 1.3?
我使用的是Visual Studio Professional 2019版本16.3.0预览版3.0,我的项目是netcore3.0
Tls13 is defined in SslProtocols
//
// Summary:
// Defines the possible versions of System.Security.Authentication.SslProtocols.
[Flags]
public enum SslProtocols {
//
// Summary:
// Allows the operating system to choose the best protocol to use, and to block
// protocols that are not secure. Unless your app has a specific reason not to,
// you should use this …Run Code Online (Sandbox Code Playgroud)