I want to use the tokio-rustls crate for encrypting traffic between client and sever for my chat-server client project 8080_msg. However I can't figure out how you go from a regular async TcpStream, to a tls connection. Could anyone help me with this? (also are there other ways to encrypt client server traffic?)
Have you tried using the tokio-rustls examples as a starting point?
I know that some people prefer being able to build things from docs alone, but I would really recommend you use the examples in the case of (tokio-)rustls. These examples aren't minimal, but they are very easy to strip down if you want to get to the bare bones of getting a TLS connection up and running. (On the other hand, in your code, you'll probably end up wanting timeouts and such, so might just as well start looking at those now).
Very basically, you perform the low-level (plain) connection as usual, and then you wrap the low-level connection in tokio-rustls wrappers.
Thanks for sharing those examples! That really helped me! The only thing I still don't understand is where the certificates come from. And do the client and server need to have the same certificate?
In the basic, and I would say common, configuration the client just needs to know that it is authenticating against what was intended. For this configuration:
The client needs the CA certificate (chain) (and the specific name of the server against which it is connecting).
The server needs a server certificate and its private key. These must be signed by the same CA.
There's another configuration where you have mutual authentication. In this configuration the client needs a client certificate and its key as well (this too must be signed by the same CA). I would suggest you ignore mutual authentication for now.
tokio-rustls tries to be the glue between tokio and rustls, but it does not strive to be more than it needs to be. That is to say, many of the operations that doesn't actually involve concrete communication is done via rustls. So you're basically going to keep to keep both tokio-rustls and rustls documentation open, side-by-side.
To answer your specific question. Rather than have the rustls runtime fetch the required PKI files, you instead load them into a root certificate store. The rustls documentation has a good section on this.
Also, I strongly suggest you look at the rustls examples (Note: The API for rustls is changing, so you should probably look at an older tagged version of the example). More to the point: Look at simple-client.rs and simple-server.rs. They are quite minimal, and demonstrate how to set up the appropriate PKI files.
If you need to actually create dummy certificates, you can use the qcert crate (which is a command line tool) or quickcert if you want to add test certificate generation to your application/library. (These are for testing only, when you're putting it into production you should probably use Let's Encrypt).