Search code examples
pythonsslopensslpyopenssl

Reading application data from a TLS connection in Python


I have used PyOpenSSL to create a connection object, and I have managed to set up a connection with a library I am testing.

I am using the bio_read and bio_write methods as I am not using a socket to transmit the data.

I now want to pass encrypted application data to the connection object, and retrieve the unencrypted application data back from the connection object.

Here is a code snipet to demonstrate my problem:

# Prints "SSL negotiation finished successfully"
print(conn.state_string());

# I give the connection 37 bytes of data representing encrypted app data
conn.bio_write("\x17\x03\x01...")

# I ask the connection to receive the data written to the buffer
conn.recv(65536)

What do I do next with the connection object? I've tried conn.read, but although it exists it is not documented, and conn.bio_read returns a WantReadError.

Thanks for the help.


Solution

  • I can't quite remember what I was doing wrong as I am no longer at work, but conn.recv(n) returns the application data. I believe the reason conn.read exists but is undocumented is that the object wraps a socket object, and so this method is a method of the socket and not OpenSSL.SSL.Connection. This would also explain why I receive a read is not a member of NoneType when I call it.