Skip to content

Commit 7c56174

Browse files
Update the APIs used in the transport 2.0 secure documentation (#786)
Co-authored-by: Christopher Pope <[email protected]>
1 parent 05ac144 commit 7c56174

File tree

4 files changed

+117
-115
lines changed

4 files changed

+117
-115
lines changed

transport_versioned_docs/version-2.0.0/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_versioned_docs/version-2.0.0/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_versioned_docs/version-2.0.0/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;
Lines changed: 89 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,149 @@
11
---
22
id: secure-connection
3-
title: Secure client and server
3+
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+
The Unity Transport package can be configure to encrypt the connection between the server and the client while ensuring the server's/client's authenticity.
6+
7+
Secure connections are available with editor versions 2020.3 (starting at 2020.3.34), 2021.3, and 2022.1 and above.
88

99
## Server authentication
1010

1111
:::warning Warning
12-
Be sure private keys are not included in your code base or client builds.
12+
This example uses hardcoded certificates to make understanding the process easier, but in a real deployment the server certificates should be kept separate from client builds. One way to achieve this is to put them on a separate assembly, or load them from a file on the server.
1313
:::
1414

1515
### 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.
16+
17+
In this configuration, the server will provide a certificate to the client (`certificate`) to prove its identity. The client will validate the certificate against its own root certificate (`caCertificate`) to validate its identity.
1818

1919
:::note
20-
Root certificate is also sometimes referred as CA Certificate.
20+
Root certificates are also sometimes referred to as CA certificates.
2121
:::
2222

23-
Once its identity confirmed, the server will then use the private key (`privateKey`) to establish the secure communication.
23+
Once its identity confirmed, the server will then use the private key (`privateKey`) to establish the secure communication channel.
2424

2525
### 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.
26+
27+
To use the client/server secure workflow, you need a valid certificate and the root certificate that was used to sign it. You also need the private key that has been used to create the certificate. If you don't have these, they can be generated using OpenSSL. The procedure is detailed hereafter.
2828

2929
### Generating the required keys and certificates with OpenSSL
3030

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

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.
33+
#### Generate the root certificate
34+
35+
First thing first is to generate a root private key. We will use it later on to generate the root certificate.
36+
3537
```shell
3638
openssl genrsa -out clientPrivateKeyForRootCA.pem 2048
3739
```
38-
Now that you have a private key, you can now generate the root certificate.
40+
41+
Now that you have a root private key, you can now generate the root certificate.
3942

4043
```shell
4144
openssl req -x509 -new -nodes -key clientPrivateKeyForRootCA.pem -sha256 -days 1095 -out myGameClientCA.pem
4245
```
46+
4347
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.
48+
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.
4649

50+
#### Generate the root-signed certificate to use with the server
51+
52+
Create now a private key for the server.
4753

48-
#### Generate the CA-signed certificate to use with the server
49-
Create now a private key for the server.
5054
```shell
51-
openssl genrsa -out myGameServerPrivate.pem 2048
55+
openssl genrsa -out myGameServerPrivateKey.pem 2048
5256
```
53-
From this private key, you can generate a certificate signing request.
57+
58+
From this private key, you can generate a certificate signing request.
59+
5460
```shell
55-
openssl req -new -key myGameServerPrivate.pem -out myGameServerCertificateSigningRequest.pem
61+
openssl req -new -key myGameServerPrivateKey.pem -out myGameServerCertificateSigningRequest.pem
5662
```
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`).
6063

61-
Finally, using the different files generated, we can create the certificate file the server will use to authenticate itself :
64+
You'll be prompted with the same questions as for generating the root certificate. The answers are no more important, except for the common name: it is recommended to use the server's hostname.
65+
66+
Finally, using the different files generated, we can create the certificate file the server will use to authenticate itself:
67+
6268
```shell
6369
openssl.exe x509 -req -in myGameServerCertificateSigningRequest.pem -CA myGameClientCA.pem -CAkey clientPrivateKeyForRootCA.pem -CAcreateserial -out myGameServerCertificate.pem -days 365 -sha256
6470
```
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
71+
72+
You should have now generated a total of five files. Out of these, only three will be used later on:
73+
* The content of the `myGameClientCA.pem` file will be used client-side as the `caCertificate` parameter.
74+
* On the server end, the contents of `myGameServerCertificate.pem` will be used for the `certificate` parameter.
75+
* On the server end, the contents of `myGameServerPrivateKey.pem` will be used for the `privateKey` parameter.
76+
77+
### Boilerplate file holding the secure parameters
78+
79+
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:
80+
81+
```csharp
7982
public static class SecureParameters
8083
{
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(
84+
// Use the common name you used to create the server certificate.
85+
public static readonly string ServerCommonName = "localserver";
86+
87+
public static readonly string MyGameClientCA =
88+
@"-----BEGIN CERTIFICATE-----
89+
*** Contents of myGameClientCA.pem ***
90+
-----END CERTIFICATE-----";
91+
92+
public static readonly string MyGameServerCertificate =
8393
@"-----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
94+
*** Contents of myGameServerCertificate.pem ***
95+
-----END CERTIFICATE-----";
96+
97+
public static readonly string MyGameServerPrivateKey =
98+
@"-----BEGIN RSA PRIVATE KEY-----
99+
*** Contents of myGameServerPrivateKey.pem ***
100+
-----END RSA PRIVATE KEY-----";
96101
}
97102
```
98103

99-
### Creating the Secure Server
104+
### Creating the secure server
100105

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

102-
Starting from the minimal server sample code, create a `NetworkSettings` object.
108+
```csharp
109+
void Start ()
110+
{
111+
var settings = new NetworkSettings();
112+
settings.WithSecureServerParameters(
113+
certificate: SecureParameters.MyGameServerCertificate,
114+
privateKey: SecureParameters.myGameServerPrivateKey);
103115

104-
```cs
105-
private NetworkSettings settings = new NetworkSettings();
116+
// ...
117+
}
106118
```
107119

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
120+
When creating the `NetworkDriver`, pass in this `NetworkSettings` object:
121+
122+
```csharp
119123
m_Driver = NetworkDriver.Create(settings);
120124
```
121-
That's it for the server !
122125

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

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-
```
128+
### Creating a secure client
129129

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
130+
The secure client is very similar to the secure server. The only difference is in how the `NetworkSettings` object is configured.
131+
132+
```csharp
133133
void Start ()
134-
{
135-
settings.WithSecureClientParameters(
136-
serverName: ref SecureParameters.ServerCommonName,
137-
caCertificate: ref SecureParameters.MyGameClientCA, // Use the content of myGameClientCA.pem
138-
);
134+
{
135+
var settings = new NetworkSettings();
136+
settings.WithSecureServerParameters(
137+
serverName: SecureParameters.ServerCommonName,
138+
caCertificate: SecureParameters.MyGameClientCA);
139+
m_Driver = NetworkDriver.Create(settings);
140+
141+
// ...
142+
}
139143
```
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.
144+
145+
You should now have a secure connection between the server and its clients!
146146

147147
:::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.
148+
If you create clients for multiple platforms, it is important for all of these to still use the same root certificate if they communicate with the same server.
149149
:::

0 commit comments

Comments
 (0)