Coverage for aiocoap/transports/tls.py: 88%
17 statements
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-28 12:34 +0000
« prev ^ index » next coverage.py v7.6.8, created at 2024-11-28 12:34 +0000
1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors
2#
3# SPDX-License-Identifier: MIT
5"""
6CoAP-over-TLS transport (early work in progress)
8Right now this is running on self-signed, hard-coded certificates with default
9SSL module options.
11To use this, generate keys as with::
13 $ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 5 -nodes
15and state your hostname (eg. localhost) when asked for the Common Name.
16"""
18from .tcp import TCPClient, TCPServer
20from aiocoap import COAPS_PORT
23class _TLSMixIn:
24 _scheme = "coaps+tcp"
25 _default_port = COAPS_PORT
28class TLSServer(_TLSMixIn, TCPServer):
29 @classmethod
30 async def create_server(cls, bind, tman, log, loop, server_context):
31 return await super().create_server(
32 bind, tman, log, loop, _server_context=server_context
33 )
36class TLSClient(_TLSMixIn, TCPClient):
37 def _ssl_context_factory(self, hostinfo):
38 c = self.credentials.ssl_client_context(self._scheme, hostinfo)
39 if c is None:
40 import ssl
42 c = ssl.create_default_context()
43 c.set_alpn_protocols(["coap"])
44 return c