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

OpenSSL::PKey convenience methods #373

Closed
wants to merge 2 commits into from

Conversation

bdewater
Copy link
Contributor

Implementing #329 (comment) and Marshal support. I'm not very good at C, so any feedback is very welcome.

len = EVP_PKEY_size(pkey);
unsigned char str[len];

return EVP_PKEY_get_raw_private_key(pkey, str, &len) == 1 ? Qtrue : Qfalse;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure I'm not doing this right. According to https://www.openssl.org/docs/man1.1.1/man3/EVP_PKEY_get_raw_private_key.html:

EVP_PKEY_get_raw_private_key() fills the buffer provided by priv with raw private key data. The size of the priv buffer should be in *len on entry to the function, and on exit *len is updated with the number of bytes actually written. If the buffer priv is NULL then *len is populated with the number of bytes required to hold the key.

However if I pass NULL instead of str the tests fail 🤔

int nid = OBJ_sn2nid(StringValueCStr(type));
if(!nid) ossl_raise(ePKeyError, "unknown OID `%"PRIsVALUE"'", type);

size_t keylen = RSTRING_LEN(key);
Copy link
Member

Choose a reason for hiding this comment

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

Variable declaration after statements is a C99 feature. Unfortunately we can't use it until we drop support for Ruby 2.6.

EVP_PKEY *pkey;

int nid = OBJ_sn2nid(StringValueCStr(type));
if(!nid) ossl_raise(ePKeyError, "unknown OID `%"PRIsVALUE"'", type);
Copy link
Member

Choose a reason for hiding this comment

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

Please use EVP_PKEY_asn1_find_str() instead of OBJ_sn2nid().

pkey_generate() is currently doing this. I think it should be extracted as a function.

@rhenium
Copy link
Member

rhenium commented May 21, 2020

EVP_PKEY_new_raw_private_key()/EVP_PKEY_get_raw_private_key() family is for pkey types that support "raw public/private key", such as X25519 (but not RSA, for example). It can't be used to implement #private?.

I think it can use OSSL_PKEY_SET_PRIVATE() macro to mark the pkey contains private components or not, similar to OpenSSL::Engine#load_private_key, since a private key will not suddenly become a public key, or vice versa.

OpenSSL::PKey.read and PKey::*.new can do the same. Anyway, I think this is out of scope of this Pull Request.

ext/openssl/ossl_pkey.c Outdated Show resolved Hide resolved
@bdewater bdewater force-pushed the pkey-convenience-methods branch 2 times, most recently from 44e7735 to cbc53fc Compare May 24, 2020 15:00
@rhenium
Copy link
Member

rhenium commented Jul 12, 2023

This was superseded by #646, and it's been merged to master now. Thank you!

@rhenium rhenium closed this Jul 12, 2023
@bdewater bdewater deleted the pkey-convenience-methods branch July 12, 2023 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants