@@ -1367,34 +1367,33 @@ private void ConfigureSimulatorForUtp1()
1367
1367
}
1368
1368
#endif
1369
1369
1370
- private FixedString4096Bytes m_ServerPrivate ;
1371
- private FixedString4096Bytes m_ServerCertificate ;
1370
+ private string m_ServerPrivateKey ;
1371
+ private string m_ServerCertificate ;
1372
1372
1373
- private FixedString512Bytes m_ServerCommonName ;
1374
- private FixedString4096Bytes m_ClientCertificate ;
1373
+ private string m_ServerCommonName ;
1374
+ private string m_ClientCaCertificate ;
1375
1375
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>
1376
1379
public void SetServerSecrets ( string serverCertificate , string serverPrivateKey )
1377
1380
{
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 ;
1385
1382
m_ServerCertificate = serverCertificate ;
1386
1383
}
1387
1384
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 )
1389
1394
{
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
-
1396
1395
m_ServerCommonName = serverCommonName ;
1397
- m_ClientCertificate = clientCertificate ;
1396
+ m_ClientCaCertificate = caCertificate ;
1398
1397
}
1399
1398
1400
1399
/// <summary>
@@ -1447,41 +1446,41 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
1447
1446
// log an error because we have mismatched configuration
1448
1447
Debug . LogError ( "Mismatched security configuration, between Relay and local NetworkManager settings" ) ;
1449
1448
}
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.
1458
1452
}
1459
1453
else
1460
1454
{
1461
1455
try
1462
1456
{
1463
1457
if ( NetworkManager . IsServer )
1464
1458
{
1465
- if ( m_ServerCertificate . Length == 0 ||
1466
- m_ServerPrivate . Length == 0 )
1459
+ if ( m_ServerCertificate . Length == 0 || m_ServerPrivateKey . Length == 0 )
1467
1460
{
1468
1461
throw new Exception ( "In order to use encrypted communications, when hosting, you must set the server certificate and key." ) ;
1469
1462
}
1470
- m_NetworkSettings . WithSecureServerParameters ( certificate : ref m_ServerCertificate ,
1471
- privateKey : ref m_ServerPrivate ) ;
1463
+ m_NetworkSettings . WithSecureServerParameters ( m_ServerCertificate , m_ServerPrivateKey ) ;
1472
1464
}
1473
1465
else
1474
1466
{
1475
1467
if ( m_ServerCommonName . Length == 0 )
1476
1468
{
1477
1469
throw new Exception ( "In order to use encrypted communications, clients must set the server common name." ) ;
1478
1470
}
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
+ }
1480
1479
}
1481
1480
}
1482
1481
catch ( Exception e )
1483
1482
{
1484
- Debug . LogException ( e , this ) ;
1483
+ Debug . LogException ( e , this ) ;
1485
1484
}
1486
1485
}
1487
1486
}
0 commit comments