Skip to content

Commit 0188cb8

Browse files
committed
Add wrappers for session serialization functions
1 parent 6502a0e commit 0188cb8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/OpenSSL/SSL.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,36 @@ class Session:
826826
_session: Any
827827

828828

829+
def d2i_SSL_SESSION(data: bytes) -> Session:
830+
p = _ffi.new("unsigned char[]", data)
831+
pp = _ffi.new("unsigned char **")
832+
pp[0] = p
833+
length = _ffi.cast("long", len(data))
834+
835+
session = _lib.d2i_SSL_SESSION(_ffi.NULL, pp, length)
836+
if session == _ffi.NULL:
837+
raise RuntimeError("d2i_SSL_SESSION failed")
838+
839+
pysession = Session.__new__(Session)
840+
pysession._session = _ffi.gc(session, _lib.SSL_SESSION_free)
841+
return pysession
842+
843+
def i2d_SSL_SESSION(session: Session) -> bytes:
844+
845+
length = _lib.i2d_SSL_SESSION(session._session, _ffi.NULL)
846+
if length == 0:
847+
raise RuntimeError("i2d_SSL_SESSION failed to get length")
848+
849+
pp = _ffi.new("unsigned char **")
850+
p = _ffi.new("unsigned char[]", length)
851+
pp[0] = p
852+
853+
length = _lib.i2d_SSL_SESSION(session._session, pp)
854+
if length == 0:
855+
raise RuntimeError("i2d_SSL_SESSION failed to produce DER")
856+
857+
return _ffi.buffer(p, length)[:]
858+
829859
class Context:
830860
"""
831861
:class:`OpenSSL.SSL.Context` instances define the parameters for setting

0 commit comments

Comments
 (0)