-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitrustme.py
47 lines (41 loc) · 1.5 KB
/
itrustme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import trustme # type: ignore # pip install trustme
import ssl
class ServerOnly:
def __init__ ( self, *,
server_hostname: str, # ex: 'test-host.example.org'
) -> None:
self.server_hostname = server_hostname
self.ca = trustme.CA()
self.server_cert = self.ca.issue_cert ( self.server_hostname )
def server_context ( self ) -> ssl.SSLContext:
ctx = ssl.create_default_context()
ctx.check_hostname = False
self.server_cert.configure_cert ( ctx )
self.ca.configure_trust ( ctx )
ctx.verify_mode = ssl.CERT_NONE
return ctx
def client_context ( self ) -> ssl.SSLContext:
ctx = ssl.create_default_context()
self.ca.configure_trust ( ctx )
return ctx
class ClientServer:
def __init__ ( self, *,
client_hostname: str, # ex: '[email protected]'
server_hostname: str, # ex: 'test-host.example.org'
) -> None:
self.client_hostname = client_hostname
self.server_hostname = server_hostname
self.ca = trustme.CA()
self.client_cert = self.ca.issue_cert ( self.client_hostname )
self.server_cert = self.ca.issue_cert ( self.server_hostname )
def server_context ( self ) -> ssl.SSLContext:
ctx = ssl.create_default_context ( ssl.Purpose.CLIENT_AUTH )
self.server_cert.configure_cert ( ctx )
self.ca.configure_trust ( ctx )
ctx.verify_mode = ssl.CERT_REQUIRED
return ctx
def client_context ( self ) -> ssl.SSLContext:
ctx = ssl.create_default_context()
self.ca.configure_trust ( ctx )
self.client_cert.configure_cert ( ctx )
return ctx