-
Notifications
You must be signed in to change notification settings - Fork 421
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6492f7c
Support for serializing/deserializing public keys
Lukasa 11c1019
Cleanup docstrings.
Lukasa d86f1d8
Switch to py.test assertions.
Lukasa e02c7d8
Further testing for load/dump_publickey
Lukasa e813cec
Remove :py: prefixes
Lukasa 1e9c7ab
No coverage on this branch
Lukasa 858b0a5
Add changelog for #382
Lukasa 9c590b9
Further docstring cleanups.
Lukasa c9c30a2
Cleanup types in docstrings.
Lukasa 0820ac2
Prettify the changelog.
Lukasa 25338c5
Document new publickey methods.
Lukasa 2b6bb80
Further clarify dump_publickey argument types.
Lukasa 666b8c1
Move publickey methods to top of changelog.
Lukasa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1623,6 +1623,32 @@ 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 :data:`FILETYPE_PEM` or | ||
:data:`FILETYPE_ASN1`). | ||
:param pkey: The PKey to dump. | ||
:type pkey: :class:`PKey` | ||
: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 +2430,38 @@ 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 :data:`FILETYPE_PEM`, | ||
: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. |
||
:type buffer: A Python string object, either unicode or bytestring. | ||
:return: The PKey object. | ||
:rtype: :class:`PKey` | ||
""" | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How about
:param PKey pkey: The public key to dump
?:class:
is definitely not necessary if you write type markup (and it's only the one type).