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

added method to export keying material from an ssl connection #686

Closed
wants to merge 4 commits into from
Closed
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
25 changes: 25 additions & 0 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,31 @@ def master_key(self):
_lib.SSL_SESSION_get_master_key(session, outp, length)
return _ffi.buffer(outp, length)[:]

def export_keying_material(self, label, olen, context=None):
"""
Obtain keying material for application use.

:param label - a disambiguating label string as described in RFC 5705
:param olen - the length of the exported key material in bytes
:param context - a per-association context value
:return the exported key material bytes or None
"""
outp = _no_zero_allocator("unsigned char[]", olen)
# RFC 5705: "Labels here have the same definition as in TLS, i.e., an
# ASCII string with no terminating NULL."
# So we must remove the NULL terminator.
label_buf = _ffi.new("char[]", label)[0:len(label)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to slice this? cffi will add a terminating NULL but when you pass it to SSL_export_keying_material with len(label) the terminating NULL simply won't be read.

context_buf, context_len, use_context, success = _ffi.NULL, 0, 0, 0
if context is not None:
context_buf = _ffi.new("unsigned char[]", context)
context_len = len(context)
use_context = 1
success = _lib.SSL_export_keying_material(self._ssl, outp, olen,
label_buf, len(label),
context_buf, context_len,
use_context)
return _ffi.buffer(outp, olen)[:] if success == 1 else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't return None if this fails. This should call _openssl_assert(success == 1), which will raise an exception if exporting the keying material fails.


def sock_shutdown(self, *args, **kwargs):
"""
See shutdown(2)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3354,6 +3354,21 @@ def test_memory_connect(self):
assert server_conn.client_random() != server_conn.server_random()
assert client_conn.client_random() != client_conn.server_random()

# Export key material for other uses.
cekm = client_conn.export_keying_material(b'LABEL', 32)
sekm = server_conn.export_keying_material(b'LABEL', 32)
assert cekm is not None
assert sekm is not None
assert cekm == sekm
assert len(sekm) == 32

# Export key material for other uses with additional context.
cekmc = client_conn.export_keying_material(b'LABEL', 32, b'CONTEXT')
sekmc = server_conn.export_keying_material(b'LABEL', 32, b'CONTEXT')
assert cekmc is not None
assert sekmc is not None
assert cekmc == sekmc

# Here are the bytes we'll try to send.
important_message = b'One if by land, two if by sea.'

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extras =
deps =
coverage>=4.2
cryptographyMaster: git+https://github.com/pyca/cryptography.git
cryptographyMinimum: cryptography<=1.9
cryptographyMinimum: cryptography<=2.1
setenv =
# Do not allow the executing environment to pollute the test environment
# with extra packages.
Expand Down