Skip to content

Commit 5ee6560

Browse files
author
Larah Armstrong
authored
Merge branch 'develop' into main
2 parents 243319c + 78817ef commit 5ee6560

File tree

4 files changed

+124
-115
lines changed

4 files changed

+124
-115
lines changed

transport/samples/secureclientbehaviour.cs.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ Sample code for `SecureClient`:
99
using UnityEngine;
1010
using Unity.Networking.Transport;
1111
using Unity.Networking.Transport.TLS;
12+
1213
public class SecureClientBehaviour : MonoBehaviour
1314
{
1415
public NetworkDriver m_Driver;
1516
public NetworkConnection m_Connection;
1617
public bool m_Done;
17-
private NetworkSettings settings = new NetworkSettings();
18+
1819
void Start ()
1920
{
20-
settings.WithSecureClientParameters(
21-
serverName: ref SecureParameters.ServerCommonName,
22-
caCertificate: ref SecureParameters.MyGameClientCA // Use the content of myGameClientCA.pem
23-
);
21+
var settings = new NetworkSettings();
22+
settings.WithSecureClientParameters(
23+
serverName: SecureParameters.ServerCommonName,
24+
caCertificate: SecureParameters.MyGameClientCA);
2425
m_Driver = NetworkDriver.Create(settings);
2526
m_Connection = default(NetworkConnection);
2627

@@ -74,4 +75,5 @@ public class SecureClientBehaviour : MonoBehaviour
7475
}
7576
}
7677
}
77-
}
78+
}
79+
```

transport/samples/secureparameters.cs.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@ title: SecureParameters sample
66
Sample code for `SecureParameters`:
77

88
```csharp
9-
using Unity.Collections;
109
public static class SecureParameters
11-
{
12-
public static FixedString32Bytes ServerCommonName = new FixedString32Bytes("localserver"); // Use the common name you used to define the server certificate.
10+
{
11+
// Use the common name you used to create the server certificate.
12+
public static readonly string ServerCommonName = "localserver";
1313

14-
public static FixedString4096Bytes MyGameClientCA = new FixedString4096Bytes(
15-
@"-----BEGIN CERTIFICATE-----
16-
*** REPLACE BY YOUR OWN CA Certificate ***
17-
-----END CERTIFICATE-----");
14+
public static readonly string MyGameClientCA =
15+
@"-----BEGIN CERTIFICATE-----
16+
*** REPLACE WITH YOUR OWN CA CERTIFICATE ***
17+
-----END CERTIFICATE-----";
1818

1919
#if UNITY_SERVER
20-
public static FixedString4096Bytes MyGameServerCertificate = new FixedString4096Bytes(
21-
@"-----BEGIN CERTIFICATE-----
22-
*** REPLACE BY YOUR OWN Server Certificate ***
23-
-----END CERTIFICATE-----");
20+
public static readonly string MyGameServerCertificate =
21+
@"-----BEGIN CERTIFICATE-----
22+
*** REPLACE WITH YOUR OWN SERVER CERTIFICATE ***
23+
-----END CERTIFICATE-----";
2424

25-
public static FixedString4096Bytes MyGameServerPrivate = new FixedString4096Bytes(
26-
@"-----BEGIN RSA PRIVATE KEY-----
27-
*** REPLACE BY YOUR OWN Private Key ***
28-
-----END RSA PRIVATE KEY-----");
25+
public static readonly string MyGameServerPrivateKey =
26+
@"-----BEGIN RSA PRIVATE KEY-----
27+
*** REPLACE WITH YOUR OWN SERVER PRIVATE KEY ***
28+
-----END RSA PRIVATE KEY-----";
2929
#endif
3030
}
31-
3231
```

transport/samples/secureserverbehaviour.cs.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ title: SecureServerBehaviour sample
44
---
55

66
Sample code for `SecureServerBehaviour`:
7-
```cs
7+
8+
```csharp
89
using UnityEngine;
910
using UnityEngine.Assertions;
1011
using Unity.Collections;
1112
using Unity.Networking.Transport;
1213
using Unity.Networking.Transport.TLS;
14+
1315
public class SecureServerBehaviour : MonoBehaviour
1416
{
1517
public NetworkDriver m_Driver;
@@ -19,9 +21,8 @@ public class SecureServerBehaviour : MonoBehaviour
1921
{
2022
var settings = new NetworkSettings();
2123
settings.WithSecureServerParameters(
22-
certificate: ref SecureParameters.MyGameServerCertificate, // The content of the `myGameServerCertificate.pem`
23-
privateKey: ref SecureParameters.MyGameServerPrivate // The content of `myGameServerPrivate.pem`
24-
);
24+
certificate: SecureParameters.MyGameServerCertificate,
25+
privateKey: SecureParameters.MyGameServerPrivateKey);
2526
m_Driver = NetworkDriver.Create(settings);
2627
var endpoint = NetworkEndPoint.AnyIpv4;
2728
endpoint.Port = 9001;

transport/workflow-client-server-secure.md

Lines changed: 96 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,148 +2,155 @@
22
id: secure-connection
33
title: Create secure client and server
44
---
5-
The unity transport protocol can be configure to encrypt the connection between the server and the client while ensuring the server's/client's authenticity.
6-
This secure connection relies on UnityTLS and is available with the following editor versions:
7-
`2020.3 (2020.3.34 minimum and above), 2021.3 (2021.3.1f1 minimum and above) and 2022.1 (2022.1.0b16 minimum and above)`
5+
6+
You can configure the Unity Transport package to encrypt the server-client connection while ensuring the authenticity of both the server and the client.
7+
8+
Secure connections are available with Editor versions 2020.3 (starting at 2020.3.34), 2021.3, and 2022.1 and above.
89

910
## Server authentication
1011

1112
:::warning Warning
12-
Be sure private keys are not included in your code base or client builds.
13+
This example uses hardcoded certificates to illustrate the process better. However, in an actual deployment, you should keep the server certificates separate from the client builds. You can do this by keeping the server certificates on a separate assembly or by loading the server certificates from a file on the server.
1314
:::
1415

1516
### High level authentication process
16-
In this configuration, the server will provide a certificate to the client (`certificate`) containing the server host name.
17-
The client will compare the server hostname to the one it knows (`serverName`) and will then validate the provided certificate against its own root certificate (`caCertificate`) confirming the server identity.
17+
18+
In this configuration, the server provides a certificate to the client (`certificate`) to prove its identity. The client uses its own root certificate (`caCertificate`) to validate the certificate from the server.
1819

1920
:::note
20-
Root certificate is also sometimes referred as CA Certificate.
21+
Root certificates are also sometimes referred to as CA certificates.
2122
:::
2223

23-
Once its identity confirmed, the server will then use the private key (`privateKey`) to establish the secure communication.
24+
After confirming its identity, the server uses the private key (`privateKey`) to establish a secure communication channel.
2425

2526
### Requirements
26-
To use the client server secure workflow, you need a `valid certificate` and the `root certificate` it has been generated from. You also need the `private key` that has been used to create the certificate.
27-
If you don't have these, they can be generated using OpenSSL. The procedure is detailed hereafter.
27+
28+
You need the following before you can use the client-server secure workflow:
29+
30+
- A valid certificate
31+
- The root certificate used to sign the certificate
32+
- The private key used to create the certificate
33+
34+
You can use OpenSSL to generate these if you don't have them. The procedure is detailed hereafter.
2835

2936
### Generating the required keys and certificates with OpenSSL
3037

3138
It is assumed that you have [OpenSSL](https://www.openssl.org/) installed on your machine.
3239

33-
#### Generate the Certification Authority root certificate.
34-
First thing first is to generate a private key. We will use it later on to generate the Certification Authority root certificate.
40+
#### Generate the root certificate
41+
42+
First, generate a root private key. You will need it later to generate the root certificate.
43+
3544
```shell
3645
openssl genrsa -out clientPrivateKeyForRootCA.pem 2048
3746
```
38-
Now that you have a private key, you can now generate the root certificate.
47+
48+
Now that you have a root private key, you can generate the root certificate.
3949

4050
```shell
4151
openssl req -x509 -new -nodes -key clientPrivateKeyForRootCA.pem -sha256 -days 1095 -out myGameClientCA.pem
4252
```
43-
You will be prompted to answer several questions. Most of the answers are not that important within the present context.
44-
It is however useful to use a `common name` that makes sense for you to identify this certificate amongst others.
45-
Ideally, you would want to use your domain name if you have one.
4653

54+
You will be prompted to answer several questions. Most of the answers are not that important within the present context. It is, however, useful to use a common name that makes sense for you to identify this certificate amongst others. Ideally, you would want to use your domain name if you have one.
55+
56+
#### Generate the root-signed certificate to use with the server
57+
58+
Now, create a private key for the server.
4759

48-
#### Generate the CA-signed certificate to use with the server
49-
Create now a private key for the server.
5060
```shell
51-
openssl genrsa -out myGameServerPrivate.pem 2048
61+
openssl genrsa -out myGameServerPrivateKey.pem 2048
5262
```
53-
From this private key, you can generate a certificate signing request.
63+
64+
You can use this private key to generate a certificate signing request.
65+
5466
```shell
55-
openssl req -new -key myGameServerPrivate.pem -out myGameServerCertificateSigningRequest.pem
67+
openssl req -new -key myGameServerPrivateKey.pem -out myGameServerCertificateSigningRequest.pem
5668
```
57-
You'll be prompted with the same questions as for generating the root certificate.
58-
The answers are no more important, except for the `common name` : use the host name of your server if you have one. This common name
59-
will appear in the generated certificate and will be compare by the Client against the common name it received as parameter (`serverName`).
6069

61-
Finally, using the different files generated, we can create the certificate file the server will use to authenticate itself :
70+
You'll be prompted with the same questions you answered when generating the root certificate. The answers are no more important (except for the common name: it is recommended to use the server's hostname).
71+
72+
Finally, you can create the certificate file the server will use to authenticate itself using the generated files:
73+
6274
```shell
6375
openssl.exe x509 -req -in myGameServerCertificateSigningRequest.pem -CA myGameClientCA.pem -CAkey clientPrivateKeyForRootCA.pem -CAcreateserial -out myGameServerCertificate.pem -days 365 -sha256
6476
```
65-
You should have now generated a total of five files. Out of these, only three will be used later on :
66-
* The content of the `myGameClientCA.pem` file, will be used client side as the `caCertificate` parameter.
67-
* On the server end, `myGameServerPrivate.pem` file content will be used for the `privateKey` parameter.
68-
* `myGameServerCertificate.pem` will be used by the `certificate` parameter on the server end.
69-
70-
### Boiler Plate file holding the secure parameters
71-
Create a `SecureParameters.cs` script file to hold your certificates and the private key. Place it in the same folder as the minimal server and minimal client scripts.
72-
Add the following dependencies:
73-
```cs
74-
using Unity.Collections;
75-
using Unity.Networking.Transport.TLS;
76-
```
77-
Then declare the secureParameters class and the boilerplate code that will hold your secure information:
78-
```cs
77+
78+
You should have now generated a total of five files. You only need to use the following three: `myGameClientCA.pem`, `myGameServerCertificate.pem`, and `myGameServerPrivateKey.pem`.
79+
80+
- On the client side, you need the content of the `myGameClientCA.pem` for the `caCertificate` parameter.
81+
- On the server end, you need the contents of `myGameServerCertificate.pem` for the `certificate` parameter.
82+
- On the server end, you need the contents of `myGameServerPrivateKey.pem` for the `privateKey` parameter.
83+
84+
### Boilerplate file holding the secure parameters
85+
86+
Create a `SecureParameters.cs` script file to hold your certificates and the private key. Place it in the same folder as the minimal server and minimal client scripts. Then, declare the `SecureParameters` static class and the boilerplate code that will hold your secure information:
87+
88+
```csharp
7989
public static class SecureParameters
8090
{
81-
public static FixedString32Bytes ServerCommonName = new FixedString32Bytes("server_certificate_host_name"); // Use the common name you used to define the server certificate.
82-
public static FixedString4096Bytes MyGameClientCA = new FixedString4096Bytes(
91+
// Use the common name you used to create the server certificate.
92+
public static readonly string ServerCommonName = "localserver";
93+
94+
public static readonly string MyGameClientCA =
95+
@"-----BEGIN CERTIFICATE-----
96+
*** Contents of myGameClientCA.pem ***
97+
-----END CERTIFICATE-----";
98+
99+
public static readonly string MyGameServerCertificate =
83100
@"-----BEGIN CERTIFICATE-----
84-
***
85-
-----END CERTIFICATE-----"); // This should contain the content of myGameClientCA.pem
86-
87-
public static FixedString4096Bytes MyGameServerCertificate = new FixedString4096Bytes(
88-
@"-----BEGIN CERTIFICATE-----
89-
***
90-
-----END CERTIFICATE-----");; // This should contain the content of myGameServerCertificate.pem
91-
92-
public static FixedString4096Bytes MyGameServerPrivate = new FixedString4096Bytes(
93-
@"-----BEGIN RSA PRIVATE KEY-----
94-
***
95-
-----END RSA PRIVATE KEY-----"); // This should contain the content of myGameServerPrivate.pem
101+
*** Contents of myGameServerCertificate.pem ***
102+
-----END CERTIFICATE-----";
103+
104+
public static readonly string MyGameServerPrivateKey =
105+
@"-----BEGIN RSA PRIVATE KEY-----
106+
*** Contents of myGameServerPrivateKey.pem ***
107+
-----END RSA PRIVATE KEY-----";
96108
}
97109
```
98110

99-
### Creating the Secure Server
111+
### Creating the secure server
100112

113+
Starting from the minimal server sample code, create a `NetworkSettings` object in the `Start` method and configure it as follows:
101114

102-
Starting from the minimal server sample code, create a `NetworkSettings` object.
115+
```csharp
116+
void Start ()
117+
{
118+
var settings = new NetworkSettings();
119+
settings.WithSecureServerParameters(
120+
certificate: SecureParameters.MyGameServerCertificate,
121+
privateKey: SecureParameters.myGameServerPrivateKey);
103122

104-
```cs
105-
private NetworkSettings settings = new NetworkSettings();
123+
// ...
124+
}
106125
```
107126

108-
Within the `start()` method, configure this `NetworkSettings` as following:
109-
```cs
110-
void Start ()
111-
{
112-
settings.WithSecureServerParameters(
113-
certificate: ref SecureParameters.MyGameServerCertificate, // The content of the `myGameServerCertificate.pem`
114-
privateKey: ref SecureParameters.MyGameServerPrivate // The content of `myGameServerPrivate.pem`
115-
);
116-
```
117-
Call create method of the network driver but this time, integrate the `NetworkSettings` object.
118-
```cs
127+
When creating the `NetworkDriver`, pass in this `NetworkSettings` object:
128+
129+
```csharp
119130
m_Driver = NetworkDriver.Create(settings);
120131
```
121-
That's it for the server !
122132

123-
### Creating a Secure Client
133+
That's it for the server!
124134

125-
The secure client is very similar to the secure server. Starting from the minimal client sample code, create `NetworkSettings` object.
126-
```cs
127-
private NetworkSettings settings = new NetworkSettings();
128-
```
135+
### Creating a secure client
129136

130-
The difference between client and server lies with the parameters that will be provided to the NetworkSettings object.
131-
Within the `start()` method, configure this `NetworkSettings` object as following :
132-
```cs
137+
The secure client is very similar to the secure server. The only difference is in how the `NetworkSettings` object is configured.
138+
139+
```csharp
133140
void Start ()
134-
{
135-
settings.WithSecureClientParameters(
136-
serverName: ref SecureParameters.ServerCommonName,
137-
caCertificate: ref SecureParameters.MyGameClientCA, // Use the content of myGameClientCA.pem
138-
);
141+
{
142+
var settings = new NetworkSettings();
143+
settings.WithSecureServerParameters(
144+
serverName: SecureParameters.ServerCommonName,
145+
caCertificate: SecureParameters.MyGameClientCA);
146+
m_Driver = NetworkDriver.Create(settings);
147+
148+
// ...
149+
}
139150
```
140-
Finally, call the `create` method of the network driver but this time, integrate the `NetworkSettings` object.
141-
```cs
142-
m_Driver = NetworkDriver.Create(settings);
143-
```
144-
and that's it !
145-
You should now have a secure connection between the server and its clients.
151+
152+
You should now have a secure connection between the server and its clients!
146153

147154
:::note
148-
If you create clients for multiple platform, it is important for all of these to use the same Root Certificate if they communicate with the same server.
155+
If you create clients for multiple platforms, it is important for all clients to continue using the same root certificate if they communicate with the same server.
149156
:::

0 commit comments

Comments
 (0)