Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trio example to use client cert #38

Merged
merged 5 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ coverage.xml

# Sphinx documentation
doc/_build/

# pyenv
.python-version
19 changes: 16 additions & 3 deletions docs/source/trustme-trio-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Create our fake certificates
ca = trustme.CA()
server_cert = ca.issue_cert(u"test-host.example.org")
client_cert = ca.issue_cert(u"client@example.org")


async def demo_server(server_raw_stream):
Expand All @@ -15,6 +16,13 @@ async def demo_server(server_raw_stream):
# Set up the server's SSLContext to use our fake server cert
server_cert.configure_cert(server_ssl_context)

# Set up the server's SSLContext to trust our fake CA, that signed
# our client cert, so that it can validate client's cert.
ca.configure_trust(server_ssl_context)

# Verify that client sent us their TLS cert signed by a trusted CA
server_ssl_context.verify_mode = trio.ssl.CERT_REQUIRED

server_ssl_stream = trio.ssl.SSLStream(
server_raw_stream,
server_ssl_context,
Expand All @@ -23,15 +31,20 @@ async def demo_server(server_raw_stream):

# Send some data to check that the connection is really working
await server_ssl_stream.send_all(b"x")
print("Server successfully sent data over the encrypted channel!")
print("Client cert looks like:", server_ssl_stream.getpeercert())


async def demo_client(client_raw_stream):
client_ssl_context = trio.ssl.create_default_context()

# Set up the client's SSLContext to trust our fake CA, that signed our
# server cert
# Set up the client's SSLContext to trust our fake CA, that signed
# our server cert, so that it can validate server's cert.
ca.configure_trust(client_ssl_context)

# Set up the client's SSLContext to use our fake client cert
client_cert.configure_cert(client_ssl_context)

client_ssl_stream = trio.ssl.SSLStream(
client_raw_stream,
client_ssl_context,
Expand All @@ -42,7 +55,7 @@ async def demo_client(client_raw_stream):

assert await client_ssl_stream.receive_some(1) == b"x"
print("Client successfully received data over the encrypted channel!")
print("Cert looks like:", client_ssl_stream.getpeercert())
print("Server cert looks like:", client_ssl_stream.getpeercert())


async def main():
Expand Down