Coverage for aiocoap/transports/tls.py: 88%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-15 22:10 +0000

1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors 

2# 

3# SPDX-License-Identifier: MIT 

4 

5""" 

6CoAP-over-TLS transport (early work in progress) 

7 

8Right now this is running on self-signed, hard-coded certificates with default 

9SSL module options. 

10 

11To use this, generate keys as with:: 

12 

13 $ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 5 -nodes 

14 

15and state your hostname (eg. localhost) when asked for the Common Name. 

16""" 

17 

18from .tcp import TCPClient, TCPServer 

19 

20from aiocoap import COAPS_PORT 

21 

22 

23class _TLSMixIn: 

24 _scheme = "coaps+tcp" 

25 _default_port = COAPS_PORT 

26 

27 

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 ) 

34 

35 

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 

41 

42 c = ssl.create_default_context() 

43 c.set_alpn_protocols(["coap"]) 

44 return c