简体   繁体   English

如何使用Winsock在C ++应用程序中实现安全套接字通信?

[英]How to implement secure socket communication in c++ application using winsock?

I am trying to implement secure communication between a server and client in c++. 我正在尝试在C ++中实现服务器和客户端之间的安全通信。 The limitation is that both the client and server must run on windows and have to be in c++. 限制是客户端和服务器都必须在Windows上运行,并且必须使用c ++。 This is for a research project I am working on at my university. 这是我在大学从事的研究项目。

So far I have found that SChannel is the best option, but the documentation is extremely confusing and I can not find any guides/tutorials on how to use it. 到目前为止,我已经发现SChannel是最好的选择,但是文档非常混乱,我找不到如何使用它的任何指南/教程。 I have already looked at this link https://docs.microsoft.com/en-us/windows/desktop/secauthn/creating-a-secure-connection-using-schannel but still do not understand how to get it working. 我已经查看了此链接https://docs.microsoft.com/zh-cn/windows/desktop/secauthn/creating-a-secure-connection-using-schannel,但仍然不知道如何使它正常工作。 Could someone guide me through this if this is the best way? 如果这是最好的方法,有人可以指导我进行此操作吗?

I also looked into use SSLStream using the CLR to have .net run inside of a c++ application. 我还研究了使用CLR使用SSLStream使.net在c ++应用程序内部运行。 However I can not use this because the client application is threaded and threads can't be used with CLR. 但是我不能使用它,因为客户端应用程序是线程化的,并且线程不能与CLR一起使用。

I already have a dummy client and server set up with communication between the two, I am just trying to secure and encrypt that communication. 我已经建立了一个虚拟的客户端和服务器,并在两者之间建立了通信,我只是试图保护和加密该通信。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Whichever SSL library you choose to use there are a few things you need to know as a beginner in this field: 无论您选择使用哪种SSL库,在此领域中,作为初学者,您都需要了解以下几点:

The server and client implementations will end up looking quite different in places. 服务器和客户端的实现最终将在某些地方看起来完全不同。

Your server is absolutely going to need a certificate with a private key. 您的服务器绝对需要带私钥的证书。 During development you clearly don't want to get one from Verisign or something so you need to create a self-signed certificate. 在开发过程中,您显然不希望从Verisign那里获得证书,因此您需要创建一个自签名证书。 You can do this with openssl or other tools. 您可以使用openssl或其他工具执行此操作。

The certificate consists of a private part and a public part. 该证书由私有部分和公共部分组成。 The public part needs to go to the client, and will be used to validate the connection. 公共部分需要转到客户端,并将用于验证连接。 When you are using something like SChannel the certificates (private and public) will need to be installed in the certificate stores of the server and client respectively. 当您使用诸如SChannel之类的证书时,分别需要在服务器和客户端的证书存储中安装证书(私有证书和公共证书)。

SChannel does not send or receive data for you. SChannel不为您发送或接收数据。 So the core of your implementation is going to be: when the network has data: read ciphertext from socket and write to SChannel. 因此,实现的核心将是:当网络中有数据时:从套接字读取密文并写入SChannel。 Read clear text from SChannel (if any) and pass to application. 从SChannel(如果有)中读取明文,然后传递给应用程序。 When the application has data to send, get clear text from Application and pass to SChannel. 当应用程序有要发送的数据时,请从应用程序获取明文并传递给SChannel。 Get the resulting ciphertext buffers from SChannel and write to the socket. 从SChannel获取生成的密文缓冲区,并将其写入套接字。

buffers from the internet may be partial, and negotiations and re-negotiations means there's no 1:1 mapping of passing data into SChannel and getting data out. 来自Internet的缓冲区可能是部分缓冲区,协商和重新协商意味着没有将数据传递到SChannel并取出数据的1:1映射。

You therefore can't get away with a naive implementation that calls SChannel once to pass data in, and once again to get un/encrypted data. 因此,您无法摆脱仅幼稚的实现,该实现一次调用SChannel传入数据,再一次调用未加密的数据。 There will potentially be nothing available, or a whole lot of packets to send between the client and the server, before you'll get any application bytes. 在获得任何应用程序字节之前,客户端和服务器之间可能没有可用的空间,或者有很多数据包要发送。 ie You will need some kind of state machine to keeptrack of this. 即,您将需要某种状态机来跟踪此情况。

Obviously, don't write both the client and server at the same time: Start with your client against an https server. 显然,不要同时编写客户端和服务器:首先从客户端对https服务器开始。

That's the general outline of the process - the things that confused me when I first encountered SSL and why none of the samples were nearly as simple as I had hoped them to be. 那是整个过程的概要-当我第一次遇到SSL时,让我感到困惑的是,为什么没有一个样本像我希望的那样简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM