Skip to content

Commit

Permalink
Add generate_ssl() to TLS client as helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Mar 27, 2024
1 parent 6c192f6 commit ae660f3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
43 changes: 43 additions & 0 deletions pymodbus/client/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ async def connect(self) -> bool:
)
return await self.base_connect()

@classmethod
def generate_ssl(
cls,
certfile: str | None = None,
keyfile: str | None = None,
password: str | None = None,
) -> ssl.SSLContext:
"""Generate sslctx from cert/key/password.
:param certfile: Cert file path for TLS server request
:param keyfile: Key file path for TLS server request
:param password: Password for for decrypting private key file
Remark:
- MODBUS/TCP Security Protocol Specification demands TLSv2 at least
- verify_mode is set to ssl.NONE (to allow self-assigned cer)
"""
return CommParams.generate_ssl(
False, certfile=certfile, keyfile=keyfile, password=password
)


class ModbusTlsClient(ModbusTcpClient):
"""**ModbusTlsClient**.
Expand Down Expand Up @@ -160,6 +181,28 @@ def __init__(
)
self.server_hostname = server_hostname


@classmethod
def generate_ssl(
cls,
certfile: str | None = None,
keyfile: str | None = None,
password: str | None = None,
) -> ssl.SSLContext:
"""Generate sslctx from cert/key/password.
:param certfile: Cert file path for TLS server request
:param keyfile: Key file path for TLS server request
:param password: Password for for decrypting private key file
Remark:
- MODBUS/TCP Security Protocol Specification demands TLSv2 at least
- verify_mode is set to ssl.NONE (to allow self-assigned cer)
"""
return CommParams.generate_ssl(
False, certfile=certfile, keyfile=keyfile, password=password,
)

@property
def connected(self) -> bool:
"""Connect internal."""
Expand Down
2 changes: 1 addition & 1 deletion pymodbus/transport/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def generate_ssl(
password: str | None = None,
sslctx: ssl.SSLContext | None = None,
) -> ssl.SSLContext:
"""Generate sslctx from cert/key/passwor.
"""Generate sslctx from cert/key/password.
MODBUS/TCP Security Protocol Specification demands TLSv2 at least
"""
Expand Down

0 comments on commit ae660f3

Please sign in to comment.