-
Notifications
You must be signed in to change notification settings - Fork 177
Add support for raw private/public keys #646
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
717fe3f
Add private?/public? methods and Marshal support to OpenSSL::PKey::PKey
bdewater 8f7467f
Add OpenSSL::PKey.private_new, #private_to_raw and public equivalents
bdewater 9cf27ac
Merge branch 'master' into pkey-convenience-methods
sylph01 7b599ca
Temporarily remove private?/public? methods
sylph01 4e1da3d
Temporarily remove dumping
sylph01 8e89a27
Rewrite OBJ_sn2nid() using EVP_PKEY_asn1_find_str()
sylph01 46d4fc7
Check if running OpenSSL's EVP has raw private key support
sylph01 80e9ef1
Remove method definition when OpenSSL does not support raw pkeys
sylph01 202cac7
Move #ifdev above doc comment
sylph01 b48fcab
Ensure key is a String
sylph01 e9ccf58
Fix error message to match actual operation
sylph01 26bd41d
Styling fix
sylph01 6628df1
Error check on EVP_PKEY_get_raw_private/public_key
sylph01 4d67841
Remove loading support as this might result in unreachable code
sylph01 edadf2f
Rename methods to match OpenSSL function names
sylph01 f721fd0
Fix documentation
sylph01 18623dd
Rename C function names
sylph01 c40d4fe
Styling fixes
sylph01 23262c2
Doc: key -> pkey
sylph01 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 hidden or 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 |
---|---|---|
|
@@ -628,6 +628,72 @@ ossl_pkey_initialize_copy(VALUE self, VALUE other) | |
} | ||
#endif | ||
|
||
#ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY | ||
/* | ||
* call-seq: | ||
* OpenSSL::PKey.new_raw_private_key(algo, string) -> PKey | ||
* | ||
* See the OpenSSL documentation for EVP_PKEY_new_raw_private_key() | ||
*/ | ||
|
||
static VALUE | ||
ossl_pkey_new_raw_private_key(VALUE self, VALUE type, VALUE key) | ||
{ | ||
EVP_PKEY *pkey; | ||
const EVP_PKEY_ASN1_METHOD *ameth; | ||
int pkey_id; | ||
size_t keylen; | ||
|
||
StringValue(type); | ||
StringValue(key); | ||
ameth = EVP_PKEY_asn1_find_str(NULL, RSTRING_PTR(type), RSTRING_LENINT(type)); | ||
if (!ameth) | ||
ossl_raise(ePKeyError, "algorithm %"PRIsVALUE" not found", type); | ||
EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); | ||
|
||
keylen = RSTRING_LEN(key); | ||
|
||
pkey = EVP_PKEY_new_raw_private_key(pkey_id, NULL, (unsigned char *)RSTRING_PTR(key), keylen); | ||
if (!pkey) | ||
ossl_raise(ePKeyError, "EVP_PKEY_new_raw_private_key"); | ||
|
||
return ossl_pkey_new(pkey); | ||
} | ||
#endif | ||
|
||
#ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY | ||
/* | ||
* call-seq: | ||
* OpenSSL::PKey.new_raw_public_key(algo, string) -> PKey | ||
* | ||
* See the OpenSSL documentation for EVP_PKEY_new_raw_public_key() | ||
*/ | ||
|
||
static VALUE | ||
ossl_pkey_new_raw_public_key(VALUE self, VALUE type, VALUE key) | ||
{ | ||
EVP_PKEY *pkey; | ||
const EVP_PKEY_ASN1_METHOD *ameth; | ||
int pkey_id; | ||
size_t keylen; | ||
|
||
StringValue(type); | ||
StringValue(key); | ||
ameth = EVP_PKEY_asn1_find_str(NULL, RSTRING_PTR(type), RSTRING_LENINT(type)); | ||
if (!ameth) | ||
ossl_raise(ePKeyError, "algorithm %"PRIsVALUE" not found", type); | ||
EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); | ||
|
||
keylen = RSTRING_LEN(key); | ||
|
||
pkey = EVP_PKEY_new_raw_public_key(pkey_id, NULL, (unsigned char *)RSTRING_PTR(key), keylen); | ||
if (!pkey) | ||
ossl_raise(ePKeyError, "EVP_PKEY_new_raw_public_key"); | ||
|
||
return ossl_pkey_new(pkey); | ||
} | ||
#endif | ||
|
||
/* | ||
* call-seq: | ||
* pkey.oid -> string | ||
|
@@ -816,6 +882,35 @@ ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self) | |
return do_pkcs8_export(argc, argv, self, 0); | ||
} | ||
|
||
#ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY | ||
/* | ||
* call-seq: | ||
* pkey.raw_private_key => string | ||
* | ||
* See the OpenSSL documentation for EVP_PKEY_get_raw_private_key() | ||
*/ | ||
|
||
static VALUE | ||
ossl_pkey_raw_private_key(VALUE self) | ||
{ | ||
EVP_PKEY *pkey; | ||
VALUE str; | ||
size_t len; | ||
|
||
GetPKey(self, pkey); | ||
if (EVP_PKEY_get_raw_private_key(pkey, NULL, &len) != 1) | ||
ossl_raise(ePKeyError, "EVP_PKEY_get_raw_private_key"); | ||
str = rb_str_new(NULL, len); | ||
|
||
if (EVP_PKEY_get_raw_private_key(pkey, (unsigned char *)RSTRING_PTR(str), &len) != 1) | ||
ossl_raise(ePKeyError, "EVP_PKEY_get_raw_private_key"); | ||
|
||
rb_str_set_len(str, len); | ||
|
||
return str; | ||
} | ||
#endif | ||
|
||
VALUE | ||
ossl_pkey_export_spki(VALUE self, int to_der) | ||
{ | ||
|
@@ -865,6 +960,35 @@ ossl_pkey_public_to_pem(VALUE self) | |
return ossl_pkey_export_spki(self, 0); | ||
} | ||
|
||
#ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY | ||
/* | ||
* call-seq: | ||
* pkey.raw_public_key => string | ||
* | ||
* See the OpenSSL documentation for EVP_PKEY_get_raw_public_key() | ||
*/ | ||
|
||
static VALUE | ||
ossl_pkey_raw_public_key(VALUE self) | ||
{ | ||
EVP_PKEY *pkey; | ||
VALUE str; | ||
size_t len; | ||
|
||
GetPKey(self, pkey); | ||
if (EVP_PKEY_get_raw_public_key(pkey, NULL, &len) != 1) | ||
ossl_raise(ePKeyError, "EVP_PKEY_get_raw_public_key"); | ||
str = rb_str_new(NULL, len); | ||
|
||
if (EVP_PKEY_get_raw_public_key(pkey, (unsigned char *)RSTRING_PTR(str), &len) != 1) | ||
ossl_raise(ePKeyError, "EVP_PKEY_get_raw_public_key"); | ||
|
||
rb_str_set_len(str, len); | ||
|
||
return str; | ||
} | ||
#endif | ||
|
||
/* | ||
* call-seq: | ||
* pkey.compare?(another_pkey) -> true | false | ||
|
@@ -1602,6 +1726,10 @@ Init_ossl_pkey(void) | |
rb_define_module_function(mPKey, "read", ossl_pkey_new_from_data, -1); | ||
rb_define_module_function(mPKey, "generate_parameters", ossl_pkey_s_generate_parameters, -1); | ||
rb_define_module_function(mPKey, "generate_key", ossl_pkey_s_generate_key, -1); | ||
#ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY | ||
rb_define_module_function(mPKey, "new_raw_private_key", ossl_pkey_new_raw_private_key, 2); | ||
rb_define_module_function(mPKey, "new_raw_public_key", ossl_pkey_new_raw_public_key, 2); | ||
#endif | ||
|
||
rb_define_alloc_func(cPKey, ossl_pkey_alloc); | ||
rb_define_method(cPKey, "initialize", ossl_pkey_initialize, 0); | ||
|
@@ -1617,6 +1745,10 @@ Init_ossl_pkey(void) | |
rb_define_method(cPKey, "private_to_pem", ossl_pkey_private_to_pem, -1); | ||
rb_define_method(cPKey, "public_to_der", ossl_pkey_public_to_der, 0); | ||
rb_define_method(cPKey, "public_to_pem", ossl_pkey_public_to_pem, 0); | ||
#ifdef HAVE_EVP_PKEY_NEW_RAW_PRIVATE_KEY | ||
rb_define_method(cPKey, "raw_private_key", ossl_pkey_raw_private_key, 0); | ||
rb_define_method(cPKey, "raw_public_key", ossl_pkey_raw_public_key, 0); | ||
#endif | ||
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. Can we group the |
||
rb_define_method(cPKey, "compare?", ossl_pkey_compare, 1); | ||
|
||
rb_define_method(cPKey, "sign", ossl_pkey_sign, -1); | ||
|
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.