Skip to main content

TLS Server

Creating a TLS server for outstation instances is exactly the same process as a TCP server, except that an extra TlsServerConfig and an AuthorizationHandler is required. For more details about TLS support and the configuration options, check the TLS general information page.

tip

A server mode of operation is also supported which does not require the client certificate to contain the role extension.

The example only demonstrate the Server.CreateTlsWithAuthz method, but there is a Server.CreateTls which does NOT take an AuthorizationHandler parameter and allows an authenticated client to perform any Modbus operation.

Examples

Certificate chain configuration

let handler =
SimpleHandler::new(vec![false; 10], vec![false; 10], vec![0; 10], vec![0; 10]).wrap();

// map unit ids to a handler for processing requests
let map = ServerHandlerMap::single(UnitId::new(1), handler.clone());

let tls_config = TlsServerConfig::new(
Path::new("./certs/ca_chain/ca_cert.pem"),
Path::new("./certs/ca_chain/server_cert.pem"),
Path::new("./certs/ca_chain/server_key.pem"),
None, // no password
MinTlsVersion::V1_2,
CertificateMode::AuthorityBased,
)?;

let server = rodbus::server::spawn_tls_server_task_with_authz(
1,
"127.0.0.1:802".parse()?,
map,
ReadOnlyAuthorizationHandler::create(),
tls_config,
AddressFilter::Any,
DecodeLevel::default(),
)
.await?;

Self-signed certificate configuration

let handler =
SimpleHandler::new(vec![false; 10], vec![false; 10], vec![0; 10], vec![0; 10]).wrap();

// map unit ids to a handler for processing requests
let map = ServerHandlerMap::single(UnitId::new(1), handler.clone());

let tls_config = TlsServerConfig::new(
Path::new("./certs/self_signed/entity1_cert.pem"),
Path::new("./certs/self_signed/entity2_cert.pem"),
Path::new("./certs/self_signed/entity2_key.pem"),
None, // no password
MinTlsVersion::V1_2,
CertificateMode::SelfSigned,
)?;

let server = rodbus::server::spawn_tls_server_task_with_authz(
1,
"127.0.0.1:802".parse()?,
map,
ReadOnlyAuthorizationHandler::create(),
tls_config,
AddressFilter::Any,
DecodeLevel::default(),
)
.await?;