Skip to content

Commit 8b2a8b7

Browse files
refactor: Use string-based encryption APIs in UnityTransport (#2238)
* Add missing XML documentation * Use string-based secure APIs from UTP Co-authored-by: ashwini <[email protected]>
1 parent 26577d6 commit 8b2a8b7

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

com.unity.netcode.gameobjects/Runtime/Transports/UTP/SecretsLoaderHelper.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ private void Awake()
8383
[Tooltip("Hostname")]
8484
[SerializeField]
8585
private string m_ServerCommonName = "localhost";
86+
87+
/// <summary>Common name of the server (typically its hostname).</summary>
8688
public string ServerCommonName
8789
{
8890
get => m_ServerCommonName;
@@ -92,6 +94,8 @@ public string ServerCommonName
9294
[Tooltip("Client CA filepath. Useful with self-signed certificates")]
9395
[SerializeField]
9496
private string m_ClientCAFilePath = "Assets/Secure/myGameClientCA.pem";
97+
98+
/// <summary>Client CA filepath. Useful with self-signed certificates</summary>
9599
public string ClientCAFilePath
96100
{
97101
get => m_ClientCAFilePath;
@@ -101,6 +105,11 @@ public string ClientCAFilePath
101105
[Tooltip("Client CA Override. Only useful for development with self-signed certificates. Certificate content, for platforms that lack file access (WebGL)")]
102106
[SerializeField]
103107
private string m_ClientCAOverride = "";
108+
109+
/// <summary>
110+
/// Client CA Override. Only useful for development with self-signed certificates.
111+
/// Certificate content, for platforms that lack file access (WebGL)
112+
/// </summary>
104113
public string ClientCAOverride
105114
{
106115
get => m_ClientCAOverride;
@@ -110,21 +119,28 @@ public string ClientCAOverride
110119
[Tooltip("Server Certificate filepath")]
111120
[SerializeField]
112121
private string m_ServerCertificateFilePath = "Assets/Secure/myGameServerCertificate.pem";
122+
123+
/// <summary>Server Certificate filepath</summary>
113124
public string ServerCertificateFilePath
114125
{
115126
get => m_ServerCertificateFilePath;
116127
set => m_ServerCertificateFilePath = value;
117128
}
118-
[Tooltip("Server Private Keyfilepath")]
129+
130+
[Tooltip("Server Private Key filepath")]
119131
[SerializeField]
120132
private string m_ServerPrivateFilePath = "Assets/Secure/myGameServerPrivate.pem";
133+
134+
/// <summary>Server Private Key filepath</summary>
121135
public string ServerPrivateFilePath
122136
{
123137
get => m_ServerPrivateFilePath;
124138
set => m_ServerPrivate = value;
125139
}
126140

127141
private string m_ClientCA;
142+
143+
/// <summary>CA certificate used by the client.</summary>
128144
public string ClientCA
129145
{
130146
get
@@ -137,13 +153,19 @@ public string ClientCA
137153
}
138154
set => m_ClientCA = value;
139155
}
156+
140157
private string m_ServerCertificate;
158+
159+
/// <summary>Certificate used by the server.</summary>
141160
public string ServerCertificate
142161
{
143162
get => ReadFile(m_ServerCertificateFilePath, "Server Certificate");
144163
set => m_ServerCertificate = value;
145164
}
165+
146166
private string m_ServerPrivate;
167+
168+
/// <summary>Private key used by the server.</summary>
147169
public string ServerPrivate
148170
{
149171
get => ReadFile(m_ServerPrivateFilePath, "Server Key");

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,34 +1367,33 @@ private void ConfigureSimulatorForUtp1()
13671367
}
13681368
#endif
13691369

1370-
private FixedString4096Bytes m_ServerPrivate;
1371-
private FixedString4096Bytes m_ServerCertificate;
1370+
private string m_ServerPrivateKey;
1371+
private string m_ServerCertificate;
13721372

1373-
private FixedString512Bytes m_ServerCommonName;
1374-
private FixedString4096Bytes m_ClientCertificate;
1373+
private string m_ServerCommonName;
1374+
private string m_ClientCaCertificate;
13751375

1376+
/// <summary>Set the server parameters for encryption.</summary>
1377+
/// <param name="serverCertificate">Public certificate for the server (PEM format).</param>
1378+
/// <param name="serverPrivateKey">Private key for the server (PEM format).</param>
13761379
public void SetServerSecrets(string serverCertificate, string serverPrivateKey)
13771380
{
1378-
if (serverPrivateKey.Length > m_ServerPrivate.Capacity ||
1379-
serverCertificate.Length > m_ServerCertificate.Capacity)
1380-
{
1381-
throw new Exception("Secret lengths are above what Unity Transport allows.");
1382-
}
1383-
1384-
m_ServerPrivate = serverPrivateKey;
1381+
m_ServerPrivateKey = serverPrivateKey;
13851382
m_ServerCertificate = serverCertificate;
13861383
}
13871384

1388-
public void SetClientSecrets(string serverCommonName, string clientCertificate = null)
1385+
/// <summary>Set the client parameters for encryption.</summary>
1386+
/// <remarks>
1387+
/// If the CA certificate is not provided, validation will be done against the OS/browser
1388+
/// certificate store. This is what you'd want if using certificates from a known provider.
1389+
/// For self-signed certificates, the CA certificate needs to be provided.
1390+
/// </remarks>
1391+
/// <param name="serverCommonName">Common name of the server (typically hostname).</param>
1392+
/// <param name="caCertificate">CA certificate used to validate the server's authenticity.</param>
1393+
public void SetClientSecrets(string serverCommonName, string caCertificate = null)
13891394
{
1390-
if (serverCommonName.Length > m_ServerCommonName.Capacity ||
1391-
clientCertificate?.Length > m_ClientCertificate.Capacity)
1392-
{
1393-
throw new Exception("Secret lengths are above what Unity Transport allows.");
1394-
}
1395-
13961395
m_ServerCommonName = serverCommonName;
1397-
m_ClientCertificate = clientCertificate;
1396+
m_ClientCaCertificate = caCertificate;
13981397
}
13991398

14001399
/// <summary>
@@ -1447,41 +1446,41 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
14471446
// log an error because we have mismatched configuration
14481447
Debug.LogError("Mismatched security configuration, between Relay and local NetworkManager settings");
14491448
}
1450-
else
1451-
{
1452-
if (m_UseWebSockets)
1453-
{
1454-
// Todo: new code to support Relay+WSS
1455-
throw new NotImplementedException();
1456-
}
1457-
}
1449+
1450+
// No need to to anything else if using Relay because UTP will handle the
1451+
// configuration of the security parameters on its own.
14581452
}
14591453
else
14601454
{
14611455
try
14621456
{
14631457
if (NetworkManager.IsServer)
14641458
{
1465-
if (m_ServerCertificate.Length == 0 ||
1466-
m_ServerPrivate.Length == 0)
1459+
if (m_ServerCertificate.Length == 0 || m_ServerPrivateKey.Length == 0)
14671460
{
14681461
throw new Exception("In order to use encrypted communications, when hosting, you must set the server certificate and key.");
14691462
}
1470-
m_NetworkSettings.WithSecureServerParameters(certificate: ref m_ServerCertificate,
1471-
privateKey: ref m_ServerPrivate);
1463+
m_NetworkSettings.WithSecureServerParameters(m_ServerCertificate, m_ServerPrivateKey);
14721464
}
14731465
else
14741466
{
14751467
if (m_ServerCommonName.Length == 0)
14761468
{
14771469
throw new Exception("In order to use encrypted communications, clients must set the server common name.");
14781470
}
1479-
m_NetworkSettings.WithSecureClientParameters(serverName: ref m_ServerCommonName, caCertificate: ref m_ClientCertificate);
1471+
else if (m_ClientCaCertificate == null)
1472+
{
1473+
m_NetworkSettings.WithSecureClientParameters(m_ServerCommonName);
1474+
}
1475+
else
1476+
{
1477+
m_NetworkSettings.WithSecureClientParameters(m_ClientCaCertificate, m_ServerCommonName));
1478+
}
14801479
}
14811480
}
14821481
catch(Exception e)
14831482
{
1484-
Debug.LogException(e,this);
1483+
Debug.LogException(e, this);
14851484
}
14861485
}
14871486
}

0 commit comments

Comments
 (0)