-
Notifications
You must be signed in to change notification settings - Fork 419
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
Support for serializing/deserializing public keys #382
Changes from 7 commits
6492f7c
11c1019
d86f1d8
e02c7d8
e813cec
1e9c7ab
858b0a5
9c590b9
c9c30a2
0820ac2
25338c5
2b6bb80
666b8c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1623,6 +1623,31 @@ def dump_certificate(type, cert): | |
return _bio_to_string(bio) | ||
|
||
|
||
def dump_publickey(type, pkey): | ||
""" | ||
Dump a public key to a buffer. | ||
|
||
:param type: The file type (one of :py:data:`FILETYPE_PEM` or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still :py: |
||
:data:`FILETYPE_ASN1`). | ||
:param pkey: The :class:`PKey` to dump. | ||
:return: The buffer with the dumped key in it. | ||
:rtype: bytes | ||
""" | ||
bio = _new_mem_buf() | ||
if type == FILETYPE_PEM: | ||
write_bio = _lib.PEM_write_bio_PUBKEY | ||
elif type == FILETYPE_ASN1: | ||
write_bio = _lib.i2d_PUBKEY_bio | ||
else: | ||
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") | ||
|
||
result_code = write_bio(bio, pkey._pkey) | ||
if result_code != 1: # pragma: no cover | ||
_raise_current_error() | ||
|
||
return _bio_to_string(bio) | ||
|
||
|
||
def dump_privatekey(type, pkey, cipher=None, passphrase=None): | ||
""" | ||
Dump a private key to a buffer | ||
|
@@ -2404,6 +2429,36 @@ def _read_passphrase(self, buf, size, rwflag, userdata): | |
return 0 | ||
|
||
|
||
def load_publickey(type, buffer): | ||
""" | ||
Load a public key from a buffer. | ||
|
||
:param type: The file type (one of :py:data:`FILETYPE_PEM`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still py |
||
:data:`FILETYPE_ASN1`). | ||
:param buffer: The buffer the key is stored in. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an explanation what type buffer can be. |
||
:return: The :class:`PKey` object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just make this |
||
""" | ||
if isinstance(buffer, _text_type): | ||
buffer = buffer.encode("ascii") | ||
|
||
bio = _new_mem_buf(buffer) | ||
|
||
if type == FILETYPE_PEM: | ||
evp_pkey = _lib.PEM_read_bio_PUBKEY( | ||
bio, _ffi.NULL, _ffi.NULL, _ffi.NULL) | ||
elif type == FILETYPE_ASN1: | ||
evp_pkey = _lib.d2i_PUBKEY_bio(bio, _ffi.NULL) | ||
else: | ||
raise ValueError("type argument must be FILETYPE_PEM or FILETYPE_ASN1") | ||
|
||
if evp_pkey == _ffi.NULL: | ||
_raise_current_error() | ||
|
||
pkey = PKey.__new__(PKey) | ||
pkey._pkey = _ffi.gc(evp_pkey, _lib.EVP_PKEY_free) | ||
return pkey | ||
|
||
|
||
def load_privatekey(type, buffer, passphrase=None): | ||
""" | ||
Load a private key from a buffer | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn’t bother you if there wasn’t anything else, but since there were more changes: could you put the
[#num]
in separate lines please?