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

Export keying material support #725

Merged
merged 6 commits into from
Nov 30, 2017
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Changes:

- Fixed a potential use-after-free in the verify callback and resolved a memory leak when loading PKCS12 files with ``cacerts``.
`#723 <https://github.com/pyca/pyopenssl/pull/723>`_
- Added ``Connection.export_keying_material`` for RFC 5705 compatible export of keying material.
`#725 <https://github.com/pyca/pyopenssl/pull/725>`_

----

Expand Down
24 changes: 24 additions & 0 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,30 @@ 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)
context_buf = _ffi.NULL
context_len = 0
use_context = 0
if context is not None:
context_buf = context
context_len = len(context)
use_context = 1
success = _lib.SSL_export_keying_material(self._ssl, outp, olen,
label, len(label),
context_buf, context_len,
use_context)
_openssl_assert(success == 1)
Copy link
Member

Choose a reason for hiding this comment

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

This function has error conditions on SSLv3 and some DTLS nonsense I didn't read very closely. Do we need to handle them?

return _ffi.buffer(outp, olen)[:]

def sock_shutdown(self, *args, **kwargs):
"""
See shutdown(2)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3379,6 +3379,28 @@ 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
assert cekmc != cekm
assert sekmc != sekm
# Export with alternate label
cekmt = client_conn.export_keying_material(b'test', 32, b'CONTEXT')
sekmt = server_conn.export_keying_material(b'test', 32, b'CONTEXT')
assert cekmc != cekmt
assert sekmc != sekmt

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

Expand Down