@@ -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